简体   繁体   中英

Python User Input to Trigger Different Results

I'm a bit of a beginner at Python, so bear with me :)

I was writing a program where I need user input to trigger a different response based on what the input is. This is basically what I have (not exactly b/c I have some timing stuff going on, but I doubt that's necessary to see.)

resp = input()
if "yes" in resp:
    print("resp1 is yes")
else:
    print("resp1 is no")


resp2 = input()
if "no" in resp2:
    print("resp2 is no")
else:
    print("resp2 is yes")

But I need this to have a different response if the answer is no. Right now, even when the user inputs 'no', it moves on to the answer that should belong to the first response, in this case 'yes', rather than going to 'no'.

How can I go about fixing this?

Thanks-- (and sorry if that didn't make any sense, I can try explaining better if you need!)

Crater

I could be a little confused by your question but with proper indents, it worked for me. It should look like this:

resp = input()
if "yes" in resp:
    print('whatever the yes answer is')
resp2 = input()
if "no" in resp2:
    print('whatever the no answer is')

In all honesty, there should probably only be one instance of asking the user for input, unless you explicitly require it for your case.

resp = input()

if "yes" in resp:
    # do stuff
elif "no" in resp:
    # do other stuff

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