2016-10-12 6 views
0

Ich bin neu in Python und habe Probleme beim Abrufen von Informationen zum Drucken in einer Tabelle, sobald 'n' oder 'N' eingegeben wird. Der Code ist unten. Ich vermisse wahrscheinlich etwas Einfaches, habe es aber nicht herausgefunden. Jede Hilfe wird geschätzt.Neu in Python 3 - Versuch, Ergebnisse in Tabelle zu drucken

Hier ist das Ergebnis. Wie Sie sehen können, wird nach Eingabe von N weiterhin nach der Geschwindigkeit gefragt:

Geben Sie die Geschwindigkeit in MPH.50 ein Geben Sie die gefahrene Zeit in Stunden ein.3 Haben Sie eine andere Berechnung einzugeben? (Geben Sie y für ja oder N für nein ein:) y Geben Sie die Geschwindigkeit in MPH.60 ein Geben Sie die gefahrene Zeit in Stunden ein.4 Haben Sie eine andere Berechnung einzugeben? (Geben Sie y für ja oder N für nein ein:) y Geben Sie die Geschwindigkeit in MPH.75 ein Geben Sie die in Stunden gefahrene Zeit ein.3 Haben Sie eine andere Berechnung einzugeben? (Geben Sie y für Ja oder N für Nein ein:) n Geben Sie die Geschwindigkeit in MPH ein.

'# Dieses Programm berechnet die Entfernung, die ein Fahrzeug zurückgelegt hat # in Meilen mit der Geschwindigkeit (mph) und der Anzahl der gefahrenen Stunden.

# Create a variable to represent the maxium travel time in hours. 
max_travel = 9 
min_travel = 1 
# Create a variable to represent the maximum speed. 
max_speed = 120 
# Create a variable to represent the minimum speed. 
min_speed = 1 
#Define a variable to represent continuation of the program 
another = ["y", "Y"] 
# Create a variable for saved results to be printed in table 
results = [] 

# main module 
def main(): 

    # Get the speed in MPH. 
    speed = int(input('Enter the speed in MPH.')) 

    # Validate that the speed entered is not out of range. 
    while speed > max_speed or speed < min_speed: 
     if speed > max_speed: 
      print('ERROR: the speed entered must be a lower number between 1 and 120.') 
      speed = int(input('Enter a lower speed in MPH.')) 

     if speed < min_speed: 
      print('ERROR: the speed entered must be a higher number between 1 and 120.') 
      speed = int(input('Enter a higher speed in MPH.')) 

    # Ask user to input travel time in hours 
    travel_time = int(input('Enter the time travelled in hours.')) 

    # Validate that the time travelled is within the range of 1 to 9 hours. 
    while travel_time > max_travel or travel_time < min_travel: 
     if travel_time > max_travel: 
      print('ERROR: the time must be a lower number between 1 and 9.') 
      travel_time = int(input('Enter a different time travelled in hours.')) 

    # Validate that the time travelled is within the range of 1 to 9 hours. 
     if travel_time < min_travel: 
      print('ERROR: the time must be a higher number between 1 and 9.') 
      travel_time = int(input('Enter a different time travelled in hours.')) 

    # This will cause the loop, with the exception of the first loop, 
    # to depend on a 'y' or 'Y' entry to enter any additional entries. 
    first = False 

    # Calculate again? 
    another = input('Do you have another calculation to enter? ' + \ 
      '(Enter y for yes or N for no:)') 

    # This loop will continue until something other 
    # than 'y' or 'Y' is entered when prompted. 
    while another == 'y' or 'Y': 
     main() 

    # Calculations saved for table 
    input_tuple =speed, travel_time 

    # Print the column headings in a table. 
    # Print the time travelled and the 
    # result of the distance calculation 
    print() 
    print('Hours\t\tDistance') 
    print('---------------------') 

    # Print saved entries and calculations in table 
    if another !='y' or 'Y': 
     for input_tuple in results: 

    # Calculate the distance travelled 
    distance = speed * travel_time 
    print("'%s'\t\t'%s'" %(travel_time, distance)) 

# Call the main function. 
def main(): 
""" 
"""' 
+0

einrücken mit 4 Leerzeichen auswertet. Nicht mehr. Nicht weniger. Verwenden Sie keine Tabs. – MattDMo

Antwort

0

Beachten Sie, dass der Code viele Einrückungsfehler enthält. Having said, dass Sie zumindest richtig

while another == 'y' or 'Y' 

zu

while another == 'y' or another == 'Y' 

tatsächlich or 'Y' immer True nur

sollte
+0

Vielen Dank! Ich versuche immer noch all die kleinen Nuancen aufzufangen, die alles wegwerfen können. Ich kann mir vorstellen, dass es mit Übung besser wird! –

Verwandte Themen