2016-03-31 9 views
0

Hallo, das ist mein allererster Python-Code, so einfach mit mir. Ich schreibe einen Code, der den Benutzer nach einem Prozentsatz fragt, und fragt weiter, bis eine akzeptable Eingabe vom Benutzer eingegeben wird. Wenn ich dies jedoch ausführe, bricht die while-Schleife nicht ab, egal, welche Art von Eingabe ich eintrage.While-Schleife läuft kontinuierlich in Python, gute Eingabe oder schlecht?

Hier ist mein Code:

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

Bitte nehmen Sie sich die Zeit gewünscht wird, das Einrücken zu beheben, so dass wir Ihnen helfen können. – Dzhao

Antwort

2

Ihre Einbuchtung ein wenig wackelig ist, und der Code erreicht nie den break Aussage, weil Sie continue die Schleife vor dann. Glücklicherweise können Sie verwenden, und else Schlüsselwort, damit es funktioniert:

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

yeah mein schlecht Ich immer vermasseln die Einrückung, wenn ich Code auf dieser Website eingeben. Aber danke, es hat wie ein Zauber funktioniert! – Clarisa

+0

@Clarisa Sobald Sie sich an die Einrückung gewöhnt haben, ist Python eine der am besten entworfenen Sprachen. Genieß es :) – Ben

3

Sie sind in Ihrem except die Eingänge Ihrer Daten zu überprüfen. Sie werden niemals in Ihr Inneres gelangen, es sei denn, das Gießen nach float löst einen ValueError aus.

Sie wollen einfach Ihre Bedingungen außerhalb des except Block bewegen, so können Sie die Daten überprüfen, die die float Casting passiert:

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

Ich denke (hoffe) der Editor vermasselt das, weil es ungültige Syntax ist, wie in der Frage geschrieben – TinyTheBrontosaurus

0

Sie brauchen noch nicht einmal die weiter/Pausen diese Arbeit zu machen. Sie müssen auch "Mathematik importieren", was von Bedeutung sein wird, sobald Sie schließlich aus der While-Schleife ausbrechen.

Was Sie tun müssen, ist die Einrückung zu beobachten. Das try/excepte war nicht in der richtigen Position - wenn das anzeigt, wie der Code tatsächlich geschrieben wurde, würde dies für deine nie endende while-Schleife verantwortlich sein.

Die nachfolgenden Arbeiten, wie Sie mit nur indenBehebungen und „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.") 
Verwandte Themen