首页 > 解决方案 > 触发不同结果的 Python 用户输入

问题描述

我是 Python 的初学者,所以请耐心等待 :)

我正在编写一个程序,我需要用户输入以根据输入内容触发不同的响应。这基本上就是我所拥有的(不完全是 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")

但如果答案是否定的,我需要这个有不同的回应。现在,即使用户输入“否”,它也会转到应该属于第一个响应的答案,在这种情况下是“是”,而不是转到“否”。

我该如何解决这个问题?

谢谢 - (如果这没有任何意义,我很抱歉,如果你需要,我可以尝试更好地解释!)

陨石坑

标签: pythonpython-3.xuser-input

解决方案


我可能对您的问题感到有些困惑,但是通过适当的缩进,它对我有用。它应该如下所示:

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

推荐阅读