英文:
Why does this code keep running else? python3
问题
check = input("检查 x 还是 y。")
while True:
print("输入是", check)
if check == "x":
print("跟随 x")
break
elif check == "y":
print("跟随 y")
break
else:
print("跟随其他")
break
print("结束")
英文:
check = input("check for x or y. ")
while True:
print("input was" , check)
if input == "x":
print("Following x")
break
elif input == "y":
print("Following y")
break
else:
print("Following else")
break
print("end")
I am trying to run the if statement depending on the input but regardless of input it always goes to else.
答案1
得分: 2
你正在比较input
函数,而不是调用input
函数并从键盘读取输入的结果(check
)。
check = input("检查x还是y。")
while True:
print("输入是", check)
if check == "x":
print("跟随x")
break
elif check == "y":
print("跟随y")
break
else:
print("跟随其他")
break
print("结束")
英文:
You are comparing the input
function, not the result (check
) of calling the input
function and reading the input from the keyboard
check = input("check for x or y. ")
while True:
print("input was" , check)
if check == "x":
print("Following x")
break
elif check == "y":
print("Following y")
break
else:
print("Following else")
break
print("end")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论