我试图制作一个简单的调查机器人,但我遇到了第二个if语句的问题。它只是忽略了if和elif,并直接进入else语句。我已经尝试了所有的东西,即使它可能是一个简单的解决方案,请帮助......
import sys
yes = "yes"
no = "no"
experience_good = "yes"
contact = "1"
print("How many times have you bean contacted by us in the past quarter (3 months)")
contact = sys.stdin.readline()
if contact != 0:
print("Was your experience was us good? Type a 1 for yes or 0 for no")
experience_good = sys.stdin.readline()
print("experiencence_good is", experience_good)
# The above line was just to double check the variable was inputted
correctly
if experience_good is 1:
print("Good to hear that.")
elif experience_good is 0:
print("Sorry about that. What could we be doing better?")
doing_better = sys.stdin.readline()
else:
print("Stuff's been messed up")
我得到的输出只是:
在过去的一个季度中,我们与您联系过多少次(3 个月)
3
你的经历对我们好吗?键入1表示“是”或0表示“否”
1
exp_good是1
东西搞砸了
答案 0 :(得分:1)
因为experience_good
永远不会1
!它的开始是
experience_good = "yes"
然后在其中途可能更改为
experience_good = sys.stdin.readline()
此时如果用户输入1
,变量将保留的是字符串值'1'
,而不是1
所以你需要
experience_good = int(sys.stdin.readline().strip())
答案 1 :(得分:1)
您必须使用==
进行int相等测试:
if contact != 0:
print("Was your experience was us good? Type a 1 for yes or 0 for no")
experience_good = sys.stdin.readline()
print("experiencence_good is", experience_good)
# The above line was just to double check the variable was inputted correctly
if experience_good == 1:
print("Good to hear that.")
elif experience_good == 0:
print("Sorry about that. What could we be doing better?")
doing_better = sys.stdin.readline()
else:
print("Stuff's been messed up")
将contact
和experience_good
投射到int
:
contact = int(contact)
experience_good = int(experience_good)
答案 2 :(得分:1)
缩进问题(现已编辑)。带上最后一个elif和last else阻止一个缩进向后。
if experience_good == 1:
print("Good to hear that.")
elif experience_good == 0:
print("Sorry about that. What could we be doing better?")
doing_better = sys.stdin.readline()
else:
print("Stuff's been messed up")
使用==来评估值。当你使用'是'它会比较对象。不是它的价值。