简体   繁体   中英

Can someone explain why this function asks for input three times in python?

def choose():
decision = input( 'Do you ' + colored('run', ('blue')) + ' or ' +
                  colored('attack?', ('blue')) )
return decision

while choose(): if choose() == "attack": etc. elif choose() == "run": etc.

The interpreter then asks for the input and after it is inputted it asks again, and again, then the program proceeds normally. Why is this and how can I fix it?

You call the input routine only once and save the response. Check that saved value repeatedly, rather than going back to your user each time.

choice = choose()
while choice:
    if choice == "attack":
        etc.
    elif choice == "run":
        etc.
    ...
    choice = choose()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM