Ich habe mich gefragt, ob es einen Weg gibt, diesen Code effizienter zu machen? Ich sage nur, dass ich für Python und Programmierung als Ganzes ein bisschen neu bin. Irgendwelche Tipps wären toll. Danke ist Fortschritt.Wie könnte ich das effizienter machen?
Hier ist, wo ich bekam die Aufgabe aus: http://www.101computing.net/how-old-is-your-cat/
Das Programm gerade überträgt die Alte Katze in Menschenjahre Form.
convertedAge = 0
stage = 0
question = input("Is you cat under 1 year old?.. Y/N")
if ((question.lower() == "y") or (question.lower() == "yes")):
ageOfCat = int(input("How old is your cat (in months)?")) #cat < 1 year old
if 1 <= ageOfCat <= 2:
convertedAge = "9 to 10 months"
elif ageOfCat == 3:
convertedAge = "2 to 3 years"
elif ageOfCat == 4:
convertedAge = "5 to 6 years"
elif ageOfCat == 5:
convertedAge = "8 to 9 years"
elif ageOfCat == 6:
convertedAge = "10 years"
elif 7 <= ageOfCat <= 8:
convertedAge = "13 years"
elif 8 <= ageOfCat <= 11:
convertedAge = "14 years"
print("In human years your cat is the equivalent of " + str(convertedAge) + " old.")
else:
ageOfCat = int(input("How old is your cat (in years)?")) #cat > 1 year old
if ageOfCat == 1:
convertedAge = 15
elif ageOfCat == 2:
convertedAge = 15 + 9
else:
convertedAge = 15 + 9 + ((ageOfCat-2) * 4)
print("In human years your cat is the equivalent of " + str(convertedAge) + " years old.")
Sie sollten berücksichtigen, dass die Eingabe "Antwort" nicht Y oder N ist, "ageOfCat" ist keine Ganzzahl. – GAVD
Um fair zu sein, ging es bei der Frage um Effizienz, nicht um Edge-Fälle. – Bahrom