2016-03-31 16 views
0

Merhaba, bu benim ilk Python kodum, bu yüzden benimle çıplak. Kullanıcıyı yüzde olarak soran ve kullanıcı tarafından kabul edilebilir bir girdi girilene kadar sormaya devam eden bir kod yazıyorum. Ancak bunu çalıştırdığımda, girilen döngü ne tür girdiklerim olursa olsun kırılmaz.Döngü sürekli Python'da çalışıyor, iyi giriş mi yoksa kötü mü?

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

girintilemeyi düzeltmek için zaman ayırın "ithal matematik" ile istenilen şekilde aşağıda çalışır. – Dzhao

cevap

2

Sizin girinti Biraz zayıf olduğunu ve kod break ulaşır asla deyim, daha önce continue döngüdür. Neyse ki, kullanabilir ve else kelime çalışması için:

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

numaralı soruda yazdığı gibi yanlış yazıyor. Ama teşekkürler, bir çekicilik gibi çalıştı! – Clarisa

+0

@Clarisa, girintiye alıştıktan sonra, python orada en iyi tasarlanmış dillerden biridir. Tadını çıkar :) – Ben

3

Sen except içindeki verilerinizin girdi kontrol ediyoruz:

İşte benim kodudur. float ürününe yönelik döküm, ValueError ürününü yükseltmedikçe, asla sizin özelliğinize girmeyeceksiniz.

Sadece except bloğunun dışına şartlarınızı taşımak istediğiniz, bu yüzden float casting geçer verileri kontrol edebilirsiniz:

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

Bence (umarım) editör, bu sözdiziminde yanlış bir sözdizimi olduğu için, – TinyTheBrontosaurus

0

Hatta bu işi yapmak için/devam sonlarını gerekmez. Ayrıca, zaman döngüsünden çıktıktan sonra önemli olan "matematiği içe aktarmaya" da ihtiyacınız olacak.

Yapmanız gereken şey, ihlali izlemek. Deneme/istisna pozisyon dışıydı - eğer kodun aslında hiç bitmeyen bir süre döngüsünü hesaba katacak şekilde yazıldığını yansıtırsa.

yalnızca girinti düzeltmeleri ve size yardımcı olabiliriz böylece

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.") 
İlgili konular