【发布时间】:2019-01-12 23:24:18
【问题描述】:
在发布此问题时,我已经检查了具有相似标题的其他链接。所有这些要么对我的问题没有答案,要么不适用于这段代码。例如,这里的链接:
Why is my batch script running both if and else statement when if statement matches?
说这是因为 OP 在脚本中使用了echo。在这里,我不使用它,但我仍然得到if 和else 的结果。
while True:
selection = input("Type a command or use a page selection")
if selection in ('exit','quit'):
sys.exit()
if selection in ('page 1','1'):
print("Page 1 text here")
if selection in ('page 2','2'):
print("Page 2 text here")
else:
print("Invalid command or page number")
【问题讨论】:
-
您指的是哪个
if(您的代码中有三个单独的ifs)?你使用什么输入数据,你得到什么,你期望什么? -
我复制/粘贴到python中,输入2,它只打印“第2页文本”,然后循环回到顶部。似乎工作正常。
-
@GarrGodfrey,是的,但是如果你输入
1,它会给你第 1 页和消息“无效的命令和页码”。 -
没错,就是这么写的。我想 OP 打算使用 elif
标签: python python-3.x if-statement