异步不返回欢迎功能

发布于 2025-02-12 21:04:08 字数 2070 浏览 1 评论 0原文

我正在学习使用异步功能,在这种做法中,我正在模拟与IA的对话。一切顺利,直到程序索要一个名称后才返回到欢迎函数中的nice {},它将继续访问create_user()函数以及create_user()函数完成时,它返回到欢迎函数返回到欢迎函数要求创建用户。

这是IM的输出:

>> Hello
>> Who are you?
>> What's your name? Moises
>> Tell me a user name you like: RD
>> Now tell me a password: 1234
>> User created!
>> Nice to meet you Moises!
>> would you like to create a user? y/n y
>> Nice, you have a username {user} and a password {}
>> now you are part of us!

代码:

    # practice.py

"""Simulating an IA using the asyncio function"""

import asyncio, time, os


def wait(): # To pause the conversation
    time.sleep(2)
    
async def welcome(): # the main function
    task = asyncio.create_task(data())
    task2 = asyncio.create_task(create_user())

    os.system('cls')

    print(">> Hello")
    wait()
    print(">> Who are you?")
    wait()
    return_value = await task # wait for task (data) to return the value with the name
    print(f">> Nice to meet you {return_value}!")
    wait()

    ask = input(">> would you like to create a user? y/n ") # Asking to create a user yes or no
    if ask == 'y':
        user, password  =  await task2 # waiting for task2 (create_user) to return values
        print(">> Nice, you have a username {user} and a password {}")
        wait()
        print(">> now you are part of us!")
    else:
        print(">> You can not interact with me if you don't have a user")
        wait()
        print(">> See you!")
        quit()

async def data(): # User personal data
    global name
    name = input(">> What's your name? ")
    return name
    

async def  create_user(): # User account data
    user = input(">> Tell me a user name you like: ")
    passw =input(">> Now tell me a password: ")
    wait()
    print(">> User created!")
    return user, passw
    


    

# Run the program
asyncio.run(welcome())

I'm learning to use async functions, and in this practice, I'm simulating a conversation with an IA. Everything goes fine until right after the program asks for a name but instead returns to the nice to meet you {} in the welcome function it continues to the create_user() function and when the create_user() function finishes it returns to the welcome function to ask to create a user.

THIS IS THE OUTPUT IM GETTING:

>> Hello
>> Who are you?
>> What's your name? Moises
>> Tell me a user name you like: RD
>> Now tell me a password: 1234
>> User created!
>> Nice to meet you Moises!
>> would you like to create a user? y/n y
>> Nice, you have a username {user} and a password {}
>> now you are part of us!

THE CODE:

    # practice.py

"""Simulating an IA using the asyncio function"""

import asyncio, time, os


def wait(): # To pause the conversation
    time.sleep(2)
    
async def welcome(): # the main function
    task = asyncio.create_task(data())
    task2 = asyncio.create_task(create_user())

    os.system('cls')

    print(">> Hello")
    wait()
    print(">> Who are you?")
    wait()
    return_value = await task # wait for task (data) to return the value with the name
    print(f">> Nice to meet you {return_value}!")
    wait()

    ask = input(">> would you like to create a user? y/n ") # Asking to create a user yes or no
    if ask == 'y':
        user, password  =  await task2 # waiting for task2 (create_user) to return values
        print(">> Nice, you have a username {user} and a password {}")
        wait()
        print(">> now you are part of us!")
    else:
        print(">> You can not interact with me if you don't have a user")
        wait()
        print(">> See you!")
        quit()

async def data(): # User personal data
    global name
    name = input(">> What's your name? ")
    return name
    

async def  create_user(): # User account data
    user = input(">> Tell me a user name you like: ")
    passw =input(">> Now tell me a password: ")
    wait()
    print(">> User created!")
    return user, passw
    


    

# Run the program
asyncio.run(welcome())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2025-02-19 21:04:08
try this 
# practice.py

"""Simulating an IA using the asyncio function"""

import asyncio
import os


async def wait():  # To pause the conversation
    await asyncio.sleep(2)


async def welcome():  # the main function

    os.system('clear')

    print(">> Hello")
    await wait()
    print(">> Who are you?")
    await wait()

    # wait for task (data) to return the value with the name
    return_value = asyncio.create_task(data())
    await return_value
    print(f">> Nice to meet you {return_value.result()}!")
    await wait()

    # Asking to create a user yes or no
    ask = input(">> would you like to create a user? y/n ")
    if ask == 'y':
        task2 = asyncio.create_task(create_user())
        # waiting for task2 (create_user) to return values
        user, password = await task2
        print(f">> Nice, you have a username {user} and a password {password}")
        await wait()
        print(">> now you are part of us!")
    else:
        print(">> You can not interact with me if you don't have a user")
        await wait()
        print(">> See you!")
        quit()


async def data():  # User personal data
    global name
    name = input(">> What's your name? ")
    return name


async def create_user():  # User account data
    user = input(">> Tell me a user name you like: ")
    passw = input(">> Now tell me a password: ")
    await wait()
    print(">> User created!")
    return user, passw


# Run the program
asyncio.run(welcome())
try this 
# practice.py

"""Simulating an IA using the asyncio function"""

import asyncio
import os


async def wait():  # To pause the conversation
    await asyncio.sleep(2)


async def welcome():  # the main function

    os.system('clear')

    print(">> Hello")
    await wait()
    print(">> Who are you?")
    await wait()

    # wait for task (data) to return the value with the name
    return_value = asyncio.create_task(data())
    await return_value
    print(f">> Nice to meet you {return_value.result()}!")
    await wait()

    # Asking to create a user yes or no
    ask = input(">> would you like to create a user? y/n ")
    if ask == 'y':
        task2 = asyncio.create_task(create_user())
        # waiting for task2 (create_user) to return values
        user, password = await task2
        print(f">> Nice, you have a username {user} and a password {password}")
        await wait()
        print(">> now you are part of us!")
    else:
        print(">> You can not interact with me if you don't have a user")
        await wait()
        print(">> See you!")
        quit()


async def data():  # User personal data
    global name
    name = input(">> What's your name? ")
    return name


async def create_user():  # User account data
    user = input(">> Tell me a user name you like: ")
    passw = input(">> Now tell me a password: ")
    await wait()
    print(">> User created!")
    return user, passw


# Run the program
asyncio.run(welcome())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文