2016-08-04 11 views
3

Also habe ich einen Electric Bill Calculator in Python geschrieben. Die gute Nachricht ist, dass es funktioniert! Allerdings muss ich dafür sorgen, dass es besser funktioniert. Auf die Frage nach der Anzahl der Stunden pro Tag kann der Benutzer 25 oder eine beliebige andere Zahl eingeben. Offensichtlich gibt es nicht 25 Stunden an einem Tag. Ich habe ein paar Dinge ausprobiert, aber ich kann nicht richtig funktionieren.Wie gebe ich einen Fehler msg basierend auf Benutzereingabe in meinem Programm?

Ich habe versucht, so etwas wie:

hours = input("How many hours per day? ") 
if hours > 24: 
    print("Don't be silly, theres not more than 24 hours in a day ") 
else: 
    main() 

Was ich versuche zu tun, ist das Programm einen Fehler msg machen angezeigt werden, wenn sie mehr als 24 Stunden geben, und wenn sie es tun, dann überspringen an den unteren Code, der fragt, ob sie eine andere Berechnung versuchen möchten. Und wenn sie 24 Stunden oder weniger eingeben, dann setze das Programm ohne Fehler msg fort. Ich habe ungefähr 2 Tage damit verbracht, das Problem zu beheben, habe stundenlang nach Google gesucht, vielleicht habe ich die richtige Antwort gesehen, aber es scheint nicht zu funktionieren. Ich nehme an, ich brauche irgendeine Art von wahrer Aussage, oder wenn, dann, aber so oft ich es versucht habe, ziehe ich mir jetzt die Haare aus. Der Code für das gesamte Programm unter:

def main(): 
    star = '*' * 70 
    print star 
    print ("Nick's Electric Bill Calculator") 
    print star 
    watts = input("Enter the watts of appliance, or total watts of  appliances ") 
    hours = input("Enter the number of hours that appliance(s) run per day ") 
    cost = input("Enter the cost in cents per KwH you pay for electricty ") 

    # print ("Don't be silly, there isn't more than 24 hours in a day!") 

    x = (watts * hours/1000.0 * 30) * cost 
    total = x 
    print star 
    print ("""If you use %s watts of electricity for %s hours per day, at a cost of 
%s cents per Kilo-watt hour, you will add $%s to your monthly 
electric bill""") % (watts, hours, cost, total) 
    print star 

playagain = 'yes' 
while playagain == 'yes': 
    main() 
    print('Would you like to do another Calculation? (yes or no)') 
    playagain = raw_input() 

Ich bin neu in der Programmierung habe ich nur für jede Beratung Python für ein paar Wochen, vielen Dank im Voraus gelernt.

Antwort

0

Eine optimierte Version von @JamesRusso Code zu sehen:

def main(): 
    star = '*' * 70 
    print star 
    print ("Nick's Electric Bill Calculator") 
    print star 
    watts = int(input("Enter the watts of appliance, or total watts of appliances")) 
    hours = int(input("Enter the number of hours that appliance(s) run per day")) 
    # Check that the hours number is good before getting to the cost 
    while (hours > 24) or (hours < 0): 
     print("Don't be silly, theres not more than 24 or less than 0 hours in a day ") 
     hours = int(input("Enter the number of hours that appliance(s) run per day ")) 
    cost = int(input("Enter the cost in cents per KwH you pay for electricty ")) 

    # We don't need an auxiliary variable x here 
    total = (watts * hours/1000.0 * 30) * cost 

    print star 

    print ("""If you use %s watts of electricity for %s hours per day, at a cost of %s cents per Kilo-watt hour, you will add $%s to your monthly electric bill""") % (watts, hours, cost, total) 
     print star 

if __name__ == '__main__': 
    playagain = 'yes' 
    while playagain == 'yes': 
     main() 
     print 'Would you like to do another Calculation? (yes or no)' 
     playagain = raw_input() 
     # Checking that the user's input is whether yes or no 
     while playagain not in ['yes', 'no']: 
      print "It's a yes/no question god damn it" 
      print 'Would you like to do another Calculation? (yes or no)' 
      playagain = raw_input() 
0

Ich gebe Ihre Fehlermeldung in eine if-Anweisung und den Berechnungscode in eine else. Auf diese Weise wird die Berechnung nicht ausgeführt, wenn die Stunde nicht korrekt ist, und stattdessen wird main verlassen und zur while-Schleife zurückgekehrt und der Benutzer gefragt, ob er wieder spielen möchte. Auch andere Benutzer darauf hingewiesen haben, nach dem, was ich getan habe unter Ihnen mehr Fehlerprüfung und Meldungen für negative Zahlen addieren sollen usw.

def main(): 
    star = '*' * 70 
    print star 
    print ("Nick's Electric Bill Calculator") 
    print star 
    watts = input("Enter the watts of appliance, or total watts of  appliances ") 
    hours = input("Enter the number of hours that appliance(s) run per day ") 
    cost = input("Enter the cost in cents per KwH you pay for electricty ") 

    if hours > 24: 
     print("Don't be silly, theres not more than 24 hours in a day ") 
    else: 
     x = (watts * hours/1000.0 * 30) * cost 
     total = x 
     print star 
     print ("""If you use %s watts of electricity for %s hours per day, at a cost of %s cents per Kilo-watt hour, you will add $%s to your monthly electric bill""") % (watts, hours, cost, total) 
     print star 

playagain = 'yes' 
while playagain == 'yes': 
main() 
print('Would you like to do another Calculation? (yes or no)') 
playagain = raw_input() 
+0

Sie diesen Code besser formatiert werden sollen und erklären, was Sie haben den Code geändert, anstatt Code zu verlieren –

0

Die Eingabe von dem Benutzer erhalten, ist eine string Typ, aber Sie vergleichen es zu int Typ. Das ist ein Fehler. Sie sollten die string zu int ersten Stimmen:

hours = int(input("How many hours per day? ")) 
# ----^--- 

Dann sind Sie so etwas wie tun:

while hours > 24: 
    print("Don't be silly, theres not more than 24 hours in a day ") 
    hours = int(input("How many hours per day? ")) 

Wenn Sie der Benutzer Eingabe einer Nummer nicht sicher tun sollten Sie einen try/catch statemant verwenden.

Ausgenommen hiervon sollten Sie bestätigen Sie den Code einrücken richtig

0

Meine Python Fähigkeiten sind ein wenig rostig, aber wenn ich Sachen zu tun haben Eingang wie die Überprüfung, ich in der Regel eine Struktur etwas wie folgt verwenden:

hours = 0 
while True: 
    hours = input("How many hours per day? ") 
    #first make sure they entered an integer 
    try: 
     tmp = int(hours) #this operaion throws a ValueError if a non-integer was entered 
    except ValueError: 
     print("Please enter an integer") 
     continue 
    #2 checks to make sure the integer is in the right range 
    if hours > 24: 
     print("Don't be silly, theres not more than 24 hours in a day ") 
     continue 
    if hours < 1: 
     print("Enter a positive integer") 
     continue 
    #everything is good, bail out of the loop 
    break 

Grundsätzlich beginnt es mit einer "unendlichen" Schleife. Es bekommt die Eingabe, geht alle Prüfungen durch, um zu sehen, ob es gültig ist, und trifft die Pause (die Schleife verlassend), wenn alles in Ordnung gegangen ist. Wenn die Daten in irgendeiner Weise fehlerhaft waren, informiert sie den Benutzer und startet die Schleife mit dem Fortfahren neu. hours = 0 wird außerhalb der Schleife deklariert, so dass es im Bereich außerhalb weiterhin zugänglich ist, sodass es an anderer Stelle verwendet werden kann.

Edit: eine Idee zum Beispiel Code hinzufügen, dass ich daran dachte, nach @Ohad Eytan Antwort