2016-03-31 24 views
0

嗨,这是我的第一个Python代码,与我一起裸露。我正在编写一个代码,要求用户输入一个百分比,并一直询问,直到用户输入可接受的输入。然而当我运行这个时,无论输入什么样的输入,while循环都不会中断。while循环在Python中持续运行,输入良好或不好?

这里是我的代码:

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.") 
+0

请花时间来修复缩进,使我们可以帮助你的作品如下。 – Dzhao

回答

2

你压痕有点靠不住,代码永远不会到达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 
+0

是啊我的坏我总是弄乱缩进当我在这个网站上输入代码。但是,谢谢你,它像一个魅力工作! – Clarisa

+0

@Clarisa一旦你习惯了缩进,python就是其中最优秀的语言之一。好好享受 :) – Ben

3

您检查您的except内部数据的输入。除非投射到float产生ValueError,否则你将永远不会进入你的场地。

你只是想移动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 
+0

我认为(希望)编辑搞砸了,因为它是无效的语法写在问题 – TinyTheBrontosaurus

0

你甚至都不需要继续/休息,以使这项工作。您还需要“导入数学”,一旦最终摆脱while循环,这将很重要。

你需要做的是观察凹痕。 try/except的位置不正确 - 如果这反映了代码实际上是如何编写的,那将会说明你永远不会停止while循环。

按你们的要求,只有缩进修复和“进口数学”

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.") 
相关问题