这是我必须创建的程序指令: 该计划是一名警察必须输入一个速度区'他们做的每一个班次。那个速度区域在他们输入的答案中是一个整数。然后他们得到第二个问题来填写询问他们的车速是多少?'。此输入答案也必须是整数。如果汽车行驶的速度比速度区快。他们在轮班前只进行过一次推算,然后每隔1公里就会因超过“速度区”而被罚款10美元。因此,如果汽车超过10公里,则罚款100美元。如果汽车行驶的速度低于估算的速度区域,那么没有罚单被罚款。如果任何罚单被罚款,该官员必须输入车型名称。 I.E日产,奥迪,法拉利......然后他们必须打印出来,就像警察票一样。这一切都必须在“速度区”之外重复。
这是我的代码:
fine=int(0)
un_speed_limit = 0
while not un_speed_limit:
speed_limit=int(input("What is your speed zone? "))
try:
un_speed_limit = int(speed_limit)
except ValueError:
print("That is not a number! ")
pass_car_speed=print("\nType '000' to kill program.\n\n")
while pass_car_speed !=000:
try:
pass_car_speed=int(input("What speed is the car going? "))
except ValueError:
print("Invalid number! ")
else:
if pass_car_speed==000:
break
if pass_car_speed > speed_limit:
fine=(pass_car_speed-speed_limit)*10
if pass_car_speed <= speed_limit:
print("This car is under the limit. No ticket needed! \n")
if pass_car_speed > speed_limit:
name=str(input("What is the car model name? "))
print("____________________________________________\n")
print(" | P O L I C E T I C K E T | \n")
print(" | Speed: ",pass_car_speed," \n")
print(" | Fine Cost: ",fine," \n")
print(" | Car Name: ",name," ")
print("____________________________________________\n")
代码已停止: 我对此代码的唯一常见错误是需要帮助的是此消息
ValueError: invalid literal for int() with base 10: (what ever I typed)
当我没有输入一个整数来表示你的速度区是什么时,会出现这个唯一的消息?&#39;
答案 0 :(得分:1)
此代码中存在许多错误。
这是更正版本,你应该花一些时间来分析这个。
while True:
try:
speed_limit = int(input("What is your speed zone? "))
break
except ValueError:
print("That is not a number! ")
continue
while True:
print("What speed is the car going?")
try:
pass_car_speed = int(input( "Enter speed, or 0 to quit: "))
except ValueError:
print("Invalid number! ")
continue
if not pass_car_speed: # 0 is False, non-zero True
break # quit loop (and program)
if pass_car_speed <= speed_limit:
print("This car is under the limit. No ticket needed!")
else:
fine = (pass_car_speed - speed_limit) * 10
name = input("What is the car model name? ")
print("____________________________________________")
print(" | P O L I C E T I C K E T | ")
print(" | Speed: ",pass_car_speed," ")
print(" | Fine Cost: ",fine," ")
print(" | Car Name: ",name," ")
print("____________________________________________")
print("Goodbye!")
请告诉我,如果您需要就学习中应该关注的内容提出一些建议,以便将来避免使用它们。
答案 1 :(得分:0)
您尝试将speed_limit
转换为int两次。一个被Try / Except子句包围,一个不是。只需从非冗余的呼叫中移除int()
呼叫,即行
speed_limit=int(input("What is your speed zone? "))
您似乎也指定了un_speed_limit
但从未使用它,您应该删除它并使用speed_limit
。
答案 2 :(得分:0)
我不得不改变行
pass_car_speed=print("\nType '000' to kill program.\n\n")
到
pass_car_speed=input("\nType '000' to kill program.\n\n")
让程序工作。