知道语法错误是什么吗?

时间:2015-10-08 18:35:05

标签: python

car = input("Audi TT + Licence + Tax: 4000")
car = int(car)
insurance = int(input("Insurance: 1500 "))
petrol= int(input("Petrol Per Month: 250*12 "))
dealerprep= int(input(input("Dealer Prep: 2000: "))

total = car + insurance + petrol + dealerprep

print("\nGrand Total :", total)
input("\n\nPress the enter key to exit.")

显然总数是错误的?我想知道它之前为什么这么说。可能是我糟糕的编码!

2 个答案:

答案 0 :(得分:2)

这一行错了:

dealerprep= int(input(input("Dealer Prep: 2000: "))

应该是

dealerprep= int(input("Dealer Prep: 2000: "))

答案 1 :(得分:0)

等待,直到你运行程序 - 然后输入输入。

#example.py
car = input("Audi TT + Licence + Tax: ")
car = int(car)
insurance = int(input("Insurance: "))
petrol= int(input("Petrol Per Month: "))
dealerprep= int(input("Dealer Prep: "))

total = car + insurance + petrol + dealerprep

print("\nGrand Total :", total)
input("\n\nPress the enter key to exit.")

然后跑,

$ python3 example.py 
Audi TT + Licence + Tax: 34
Insurance: 453
Petrol Per Month: 645
Dealer Prep: 64557

Grand Total : 65689

按enter键退出。 #i按回车键,程序停止运行

一个注意事项。如果要输入表达式(如45 * 20),则应将此表达式传递给eval()funxtion。

在您的情况下,如果您想每月输入" Petrol"如45*20,请尝试

petrol= eval(input("Petrol Per Month: ")) 

否则会引发ValueError

>>> c = int(input("Petrol per Month: "))
Petrol per Month: 45*20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '45*20'