我试图制作一个简单的调查机器人,但我在第二个 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")
我得到的输出只是:
How many times have you bean contacted by us in the past quarter (3 months)
3
Was your experience was us good? Type a 1 for yes or 0 for no
1
exp_good is 1
Stuff's been messed up
最佳答案
因为experience_good
永远不会1
!它开始于
experience_good = "yes"
然后中途也许改变为
experience_good = sys.stdin.readline()
此时如果用户输入1
,则该变量将保存的是字符串值'1'
,而不是1
所以你需要
experience_good = int(sys.stdin.readline().strip())
关于python - 为什么我的程序应该进入if却直接进入else block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44350986/