如何在 python 中对计算器进行输入检查?
How to put an input check for a calculator in python?
print("Welcome to the Kinetic Energy and Gravitational Potential Energy Calculator")
print("Which Calculator would you like to access?")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
while Choice != "KE" or "GPE":
print("Error. You have not entered a valid choice. Please type KE or GPE.")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
while Choice == "KE":
print("You have chosen the Kinetic Energy Calculator.")
mass = float(input("Type in the mass of the object in kilograms (kg):"))
velocity = float(input("Type in the velocity of the object in metres per second (m/s):"))
result = 0.5*mass*(velocity**2)
print("The KE is:", result, "J(Joules)")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
while Choice == "GPE":
print("You have chosen the Gravitational Potential Energy Calculator.")
mass = float(input("Type in the mass of the object in kilograms (kg):"))
height = float(input("Type in the height at which the object is in metres (m):"))
result = 9.81*mass*height
print("The GPE is:", result, "J(Joules)")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
我试过使用"or"所以检查可以包括'KE'和'GPE',但是当你运行程序时,即使我输入KE或GPE ,第一个 while 循环继续!我怎样才能让它发挥作用?
问题出在你的情况。由于"GPE"
在逻辑上是True
(非空字符串在逻辑上是True
),while循环条件总是True
,不管选择什么
变化:
Choice != "KE" or "GPE"
至:
Choice != "KE" and Choice != "GPE"
或:
Choice not in ["KE","GPE"]
print("Welcome to the Kinetic Energy and Gravitational Potential Energy Calculator")
print("Which Calculator would you like to access?")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
while Choice != "KE" or "GPE":
print("Error. You have not entered a valid choice. Please type KE or GPE.")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
while Choice == "KE":
print("You have chosen the Kinetic Energy Calculator.")
mass = float(input("Type in the mass of the object in kilograms (kg):"))
velocity = float(input("Type in the velocity of the object in metres per second (m/s):"))
result = 0.5*mass*(velocity**2)
print("The KE is:", result, "J(Joules)")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
while Choice == "GPE":
print("You have chosen the Gravitational Potential Energy Calculator.")
mass = float(input("Type in the mass of the object in kilograms (kg):"))
height = float(input("Type in the height at which the object is in metres (m):"))
result = 9.81*mass*height
print("The GPE is:", result, "J(Joules)")
Choice = str(input("Type in KE for Kinetic Energy and GPE for Gravitational Potential Energy:"))
我试过使用"or"所以检查可以包括'KE'和'GPE',但是当你运行程序时,即使我输入KE或GPE ,第一个 while 循环继续!我怎样才能让它发挥作用?
问题出在你的情况。由于"GPE"
在逻辑上是True
(非空字符串在逻辑上是True
),while循环条件总是True
,不管选择什么
变化:
Choice != "KE" or "GPE"
至:
Choice != "KE" and Choice != "GPE"
或:
Choice not in ["KE","GPE"]