我目前正在進行課堂練習評估,我想知道是否可以使用某種類型的循環縮短我的代碼?縮短重複的Python代碼
BASIC=500
accident_price = 0
total_price=0
total_price+=BASIC
age=float(input("what is your age?"))
accidents=int(input("How many accidents have you had?"))
for i in range(1):
if age < 25:
total_price=total_price+100
print("Peole under 25 pay extra 100$")
if accidents == 1:
total_price+=50
break
elif accidents == 2:
total_price+=125
break
elif accidents==3:
total_price+=225
break
elif accidents == 4:
total_price+=375
break
elif accidents == 5:
total_price+=500
break
elif accidents == 0:
print("No extra charge!")
break
if accidents > 5:
print("No insurance!")
total_price=0
if accidents < 6:
print("Your total comes to: ${}".format(total_price))
創建一個字典'accidents'。另外爲什麼'爲我在範圍(1)'? –
請注意你的for循環是完全冗餘的 – e4c5
它已經被@ e4c5說過了,但這是一個相當大的誤解:'對於我在範圍(1)中''意味着「只做一次」。這不需要任何循環。 –