2016-06-23 6 views
-3

Ich habe eine grundlegende main() - Funktion, die mit der Eingabe beginnt (Meileninformation). Ich muss die Eingabeanweisung in der Hauptfunktion (erforderlich) belassen und dann mit der Funktion milesToKM() fortfahren.
Das Ziel ist:Python 3.5.1, Wie man in einer Funktion 3 mal nach Eingabe fragt

1) Wenn der Eingang ist> = 0, dann Ergebnisse drucken und

2) Wenn der Eingang ist < 0, Zustand in der nächsten Umwandlung (Gallonen) bewegt, dass die Eingabe nicht korrekt ist und Fragen Sie noch zweimal nach Eingabe (3 Gesamtchancen). Nach 3 fehlerhaften Versuchen muss das Programm beendet werden.

Ich darf in diesem Programm nicht importieren sys (sys.exit) oder "break" verwenden.
Programm muss einfach bleiben. Was ist der einfachste Weg, diesen Code zu aktualisieren, um die benötigten Ergebnisse zu erhalten?

def main(): 
    #Get miles to be converted to kilometers 
    miles = float(input('How many miles would you like converted to kilometers?\n')) 
    milesToKm(miles) 

    #Obtain the amount of gallons to convert 
    gallons = float(input('Please give me the amount of gallons you would like to convert to liters.\n')) 
    gallonsToLiter (gallons) 

def milesToKm(value): 
    if value >= 0: 
     milesToKm = value * 1.6 # Calculation to convert miles to kilometers 
     print (value, 'miles is', format(milesToKm, ',.1f'), 'kilometers.\n') # Display kilometers 
    else: 
     print ('That is an incorrect value. Please try again.') 

def gallonsToLiter(value): 
    if value >= 0: 
     gallonsToLiter = value * 3.9 # C0nvert gallons into liters 
     print (value, 'gallons is', format(gallonsToLiter, '.1f'), 'liters.\n') # Display liters 
    else: 
     print ('That is an incorrect value. Please try again.') 

#Call the main function 
main() 
+0

Wir machen keine Hausaufgaben. – Tom

+0

Müssen Sie auch nach Nicht-Nummern suchen? Kann jemand "7fty564" als Anzahl von Meilen eingeben? –

+1

Was hast du probiert? Sie können versuchen, eine 'for tries in range (3):' Schleife zu verwenden, wenn die Ausgabe korrekt ist. Auf dieser Seite möchten wir sehen, was Sie versucht haben, und dann können wir eine spezifische Frage beantworten, die auf diesem Code basiert. Dies ist keine Website zum Erstellen von Code. –

Antwort

0

ändern main() dazu:

def main(): 
    #Get miles to be converted to kilometers 
    miles = miles = float(input('How many miles would you like converted to kilometers?\n')) 

    # Number of tries 
    numTries = 1 

    # Checks if miles is less than 0 and if less than 3 tries 
    while numTries < 3 and miles < 0: 
     # add 1 to numTries and ask user for miles again 
     numTries += 1 
     print("Oops,that statement is incorrect") 
     miles = float(input('How many miles would you like converted to kilometers?\n')) 

    # Terminates program if 3 tries 
    if numTries == 3: 
     quit() 

    #Obtain the amount of gallons to convert 
    gallons = float(input('Please give me the amount of gallons you would like to convert to liters.\n')) 
    gallonsToLiter (gallons) 
0

Könnte einfacher sein. Suchen Sie nach einem ValueError für eine sichere Eingabe.

_continue = True 
_nTries = 0 

while _continue: 
    miles = int(input("How many miles would you like converted to kilometers?\n ")) 

    if miles >= 0: 
     # 1) If input is >=0, then print results and move to the next conversion (gallons) 
    else: 
     #2) If input is <0, state that the input is incorrect and ask for input again 2 more times (3 total chances). After 3 incorrect attempts the program needs to TERMINATE. 
     _nTries += 1 

    if _nTries == 3: 
     _continue = False 
+0

Danke an alle - wieder einmal habe ich es geschafft, etwas härter zu machen, als es wirklich ist. – Linda