简体   繁体   English

Python用户输入触发不同的结果

[英]Python User Input to Trigger Different Results

I'm a bit of a beginner at Python, so bear with me :) 我是Python的初学者,所以请忍受:)

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.) 基本上就是我所拥有的(不完全是b / c,我正在进行一些计时工作,但是我怀疑这是必要的。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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