我目前正在进行课堂练习评估,我想知道是否可以使用某种类型的循环缩短我的代码?缩短重复的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)中''意味着“只做一次”。这不需要任何循环。 –