为什么这段代码一直运行 else? python3

huangapple go评论93阅读模式
英文:

Why does this code keep running else? python3

问题

  1. check = input("检查 x 还是 y。")
  2. while True:
  3. print("输入是", check)
  4. if check == "x":
  5. print("跟随 x")
  6. break
  7. elif check == "y":
  8. print("跟随 y")
  9. break
  10. else:
  11. print("跟随其他")
  12. break
  13. print("结束")
英文:
  1. check = input("check for x or y. ")
  2. while True:
  3. print("input was" , check)
  4. if input == "x":
  5. print("Following x")
  6. break
  7. elif input == "y":
  8. print("Following y")
  9. break
  10. else:
  11. print("Following else")
  12. break
  13. 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)。

  1. check = input("检查x还是y。")
  2. while True:
  3. print("输入是", check)
  4. if check == "x":
  5. print("跟随x")
  6. break
  7. elif check == "y":
  8. print("跟随y")
  9. break
  10. else:
  11. print("跟随其他")
  12. break
  13. print("结束")
英文:

You are comparing the input function, not the result (check) of calling the input function and reading the input from the keyboard

  1. check = input("check for x or y. ")
  2. while True:
  3. print("input was" , check)
  4. if check == "x":
  5. print("Following x")
  6. break
  7. elif check == "y":
  8. print("Following y")
  9. break
  10. else:
  11. print("Following else")
  12. break
  13. print("end")

huangapple
  • 本文由 发表于 2023年7月10日 17:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定