当循环在 Python 中连续运行时,输入是好是坏?
While loop runs continuously in Python, good input or bad?
我正在编写一个代码,要求用户输入一个百分比,并一直询问直到用户输入可接受的输入。然而,当我 运行 这个时,无论我输入什么样的输入,while 循环都不会中断。
这是我的代码:
import math
while True:
try:
entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
continue
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
continue
else:
#the percent entered is valid, break out of while loop
break
print("Ship is traveling at ", entered, "% the speed of light.")
print(" ")
speedOfLight = 299792458 #speed of light constant
percentage = entered / 100 #turn entered percent into decimal
speed = speedOfLight * percentage #actual speed (in m/s)
denominator = math.sqrt(1 - (percentage ** 2)) #denominator of factor equation
factor = 1 / denominator #solve for given factor equation
shipWeight = 70000 * factor #given ship weight * factor
alphaCentauri = 4.3 / factor # given times divided by the factor
barnardsStar = 6.0 / factor
betelgeuse = 309 /factor
andromeda = 2000000 / factor
print("At this speed: ")
print(" Weight of the shuttle is ", shipWeight)
print(" Perceived time to travel to Alpha Centauri is ", alphaCentauri, " years.")
print(" Perceived time to travel to Barnard's Star is ", barnardsStar, " years.")
print(" Perceived time to travel to Betelgeuse is ", betelgeuse, " years.")
print(" Perceived time to travel to Andromeda Galaxy is ", andromeda, " years.")
您正在检查 except
中的数据输入。除非对 float
的转换引发 ValueError
,否则你永远不会进入你的 except 内部。
您只是想将您的条件移到 except
块之外,这样您就可以检查通过 float
转换的数据:
while True:
try:
entered = float(input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
continue
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
continue
else:
#the percent entered is valid, break out of while loop
break
你的缩进有点不稳定,代码永远不会到达 break
语句,因为你 continue
之前的循环。幸运的是,您可以使用 else
关键字使其工作:
while True:
try:
entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
else: # no exception
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
continue
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
continue
else:
#the percent entered is valid, break out of while loop
break
您甚至不需要 continues/breaks 来完成这项工作。您还需要 "import math",一旦您最终跳出 while 循环,这将很重要。
你需要做的是注意缩进。 try/except 位置不对——如果这反映了代码的实际编写方式,那将解释你永无止境的 while 循环。
下面的工作如您所愿,只修复了缩进 "import math"
import math
valid = False
while not valid:
try:
entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
else:
#the percent entered is valid, break out of while loop
valid = True
print("Ship is traveling at ", entered, "% the speed of light.")
print(" ")
speedOfLight = 299792458 #speed of light constant
percentage = entered / 100 #turn entered percent into decimal
speed = speedOfLight * percentage #actual speed (in m/s)
denominator = math.sqrt(1 - (percentage ** 2)) #denominator of factor equation
factor = 1 / denominator #solve for given factor equation
shipWeight = 70000 * factor #given ship weight * factor
alphaCentauri = 4.3 / factor # given times divided by the factor
barnardsStar = 6.0 / factor
betelgeuse = 309 /factor
andromeda = 2000000 / factor
print("At this speed: ")
print(" Weight of the shuttle is ", shipWeight)
print(" Perceived time to travel to Alpha Centauri is ", alphaCentauri, " years.")
print(" Perceived time to travel to Barnard's Star is ", barnardsStar, " years.")
print(" Perceived time to travel to Betelgeuse is ", betelgeuse, " years.")
print(" Perceived time to travel to Andromeda Galaxy is ", andromeda, " years.")
我正在编写一个代码,要求用户输入一个百分比,并一直询问直到用户输入可接受的输入。然而,当我 运行 这个时,无论我输入什么样的输入,while 循环都不会中断。
这是我的代码:
import math
while True:
try:
entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
continue
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
continue
else:
#the percent entered is valid, break out of while loop
break
print("Ship is traveling at ", entered, "% the speed of light.")
print(" ")
speedOfLight = 299792458 #speed of light constant
percentage = entered / 100 #turn entered percent into decimal
speed = speedOfLight * percentage #actual speed (in m/s)
denominator = math.sqrt(1 - (percentage ** 2)) #denominator of factor equation
factor = 1 / denominator #solve for given factor equation
shipWeight = 70000 * factor #given ship weight * factor
alphaCentauri = 4.3 / factor # given times divided by the factor
barnardsStar = 6.0 / factor
betelgeuse = 309 /factor
andromeda = 2000000 / factor
print("At this speed: ")
print(" Weight of the shuttle is ", shipWeight)
print(" Perceived time to travel to Alpha Centauri is ", alphaCentauri, " years.")
print(" Perceived time to travel to Barnard's Star is ", barnardsStar, " years.")
print(" Perceived time to travel to Betelgeuse is ", betelgeuse, " years.")
print(" Perceived time to travel to Andromeda Galaxy is ", andromeda, " years.")
您正在检查 except
中的数据输入。除非对 float
的转换引发 ValueError
,否则你永远不会进入你的 except 内部。
您只是想将您的条件移到 except
块之外,这样您就可以检查通过 float
转换的数据:
while True:
try:
entered = float(input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
continue
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
continue
else:
#the percent entered is valid, break out of while loop
break
你的缩进有点不稳定,代码永远不会到达 break
语句,因为你 continue
之前的循环。幸运的是,您可以使用 else
关键字使其工作:
while True:
try:
entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
else: # no exception
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
continue
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
continue
else:
#the percent entered is valid, break out of while loop
break
您甚至不需要 continues/breaks 来完成这项工作。您还需要 "import math",一旦您最终跳出 while 循环,这将很重要。
你需要做的是注意缩进。 try/except 位置不对——如果这反映了代码的实际编写方式,那将解释你永无止境的 while 循环。
下面的工作如您所愿,只修复了缩进 "import math"
import math
valid = False
while not valid:
try:
entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
except ValueError:
print("Sorry, an acceptable input was not entered. Try again.")
continue
if entered > 100:
print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
elif entered <= 0:
print("Sorry, a percent cannot be negative. Try again.")
else:
#the percent entered is valid, break out of while loop
valid = True
print("Ship is traveling at ", entered, "% the speed of light.")
print(" ")
speedOfLight = 299792458 #speed of light constant
percentage = entered / 100 #turn entered percent into decimal
speed = speedOfLight * percentage #actual speed (in m/s)
denominator = math.sqrt(1 - (percentage ** 2)) #denominator of factor equation
factor = 1 / denominator #solve for given factor equation
shipWeight = 70000 * factor #given ship weight * factor
alphaCentauri = 4.3 / factor # given times divided by the factor
barnardsStar = 6.0 / factor
betelgeuse = 309 /factor
andromeda = 2000000 / factor
print("At this speed: ")
print(" Weight of the shuttle is ", shipWeight)
print(" Perceived time to travel to Alpha Centauri is ", alphaCentauri, " years.")
print(" Perceived time to travel to Barnard's Star is ", barnardsStar, " years.")
print(" Perceived time to travel to Betelgeuse is ", betelgeuse, " years.")
print(" Perceived time to travel to Andromeda Galaxy is ", andromeda, " years.")