简体   繁体   中英

Python interpreter completely skips function asking for user input?

At first I had Choice = input("Do you " + colored("run ", 'blue')+"or " + colored("attack? ", 'blue') + "") and everything worked fine, user could do input and code continued as normal. However, I wanted to change this to a function so I now have

def func(Choice): input("Do you " + colored("run ", 'blue')+"or " + colored("attack? ", 'blue') + "") and the python interpreter completely skips over the function and doesn't ask for input. I'm relatively new to python so I apologize if I did something stupid but I couldn't find any answers online. The version is 3.5.2 by the way.

You seem to be a bit confused. You need to call the function in order for it to be executed. When the interpreter encounters the def statement it makes a function according to the definition. It will never be executed unless you call it. You want something like:

def func():
    choice = input("Do you " + colored("run ", 'blue')+"or " + 
       colored("attack? ", 'blue') + "")
    return choice

Whenever you want the user's input, you write

choice = func()

or something similar. The variable doesn't need to have the same name in both places.

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