我是python的初学者,我想知道为什么我不能让elif条件返回true并执行代码。无论我如何尝试和输入选择,我总是得到“hello world”打印,而不是“我统治世界”
name = input("What is your name?")
print("Hello " + name)
choice = input('option 1: say Hello World or Option 2: say I rule the world?')
if choice == "option 1" or "1":
counter = 0
while counter < 50:
print("hello world")
counter += 1
elif choice == "option 2" or "2":
counter = 0
while counter < 50:
print("I rule the world")
counter += 1
这应该可以工作,因为您的if语句已关闭。您也应该选择小写来进行比较
代码的问题在于以下行:
这里您并不是在看
choice
是“option 1”还是“1”,而是如果它等于“option 1”,或者字符串值“1”,它的计算结果是true
,因此,您总是得到第一个循环它应该是这样的:
elif的行也是如此:
如果运行以下代码:
您将看到输出为
true
相关问题 更多 >
编程相关推荐