2016-03-31 60 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.") 
相關問題