für meine Informatics-Klassenzuordnung bittet es mich, eine try/except/else-Struktur zu meinem Code zu verwenden. Ich weiß, dass ich auf dieser Website keine anfängerfreundlichen Fragen stellen sollte, aber ich brauche Hilfe.Wie würde ich die try/except/else-Struktur in meinen Code implementieren?
Überprüfen Sie, ob der Benutzer eine gültige Monatsnummer und eine gültige Tagesnummer eingibt. Verwenden Sie eine try/except/else-Struktur, um sicherzustellen, dass numerische Daten eingegeben werden. Ich habe bereits die if/else-Struktur.
Ich weiß nicht, ob die Frage mich fragt, einen von ihnen oder alle drei zu verwenden.
Hier ist mein Code und es funktioniert völlig in Ordnung:
#This program will ask the user to enter a month (in numeric form), a day in a months, and a two-digit year.
#Then, determine if this is a special date(the month times the day equals the year).
#Special Date
print("The date February 10, 2020 is special because when it is written in the following format the month times the day equals the year : 2/10/20.")
#Inputs
userInputMonth = int(input("Please enter a valid month:"))
userInputDay = int(input("Please enter a valid day:"))
userInputYear = int(input("Please enter a valid two-digit-year:"))
print()
if userInputMonth * userInputDay == userInputYear:
print("The date you provided " + str(userInputMonth) + "/" + str(userInputDay) + "/" + \
str(userInputYear) + " is the special date.")
else:
print("The date you provided " + str(userInputMonth) + "/" + str(userInputDay) + "/" + \
str(userInputYear) + " is not the special date.")
Ich muss nur herausfinden, wie die try/except/else
Struktur zu implementieren, um sicherzustellen, dass seine ein gültiger Monat gültig Tag, gültig Jahr.
Pastebin.com/MbWsjD7H Hier ist, was ich jetzt habe, Wenn ich einen ungültigen Wert eingeben, geht es einfach zur nächsten Zeile und ignoriert sie. Ich versuche es zu wiederholen, bis es korrekt ist. – GeForce
@GeForce siehe den Bearbeitungsbereich meiner Antwort. –