异步不返回欢迎功能
我正在学习使用异步功能,在这种做法中,我正在模拟与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)