[英]Asyncio don't return to the welcome function
我正在學習使用異步函數,在這個練習中,我正在模擬與 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要求創建用戶。
>> 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())
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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.