2015-10-08 39 views
-1
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.") 

顯然總數是錯誤的?我想知道它爲什麼這樣說,因爲它以前沒有。可能是我可憐的編碼!任何想法的語法錯誤是什麼?

+0

你試圖值傳遞給'input'通過提示?而且,'input(input(...))'沒有多大意義。 – ForceBru

+2

'輸入(輸入(「我應該問你什麼問題?))':p – spectras

+3

@ForceBru冷靜的完成,我只是剛剛開始編程,所以我一定會弄錯事情! –

回答

2

此行是錯誤的:

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

應該

dealerprep= int(input("Dealer Prep: 2000: ")) 
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 

按回車鍵退出。 #i按回車並且程序停止運行

一注意。如果你要輸入一個表達式(比如45 * 20),你應該把這個表達式傳遞給eval()funxtion。

在你的情況,如果你想輸入「每月汽油」像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' 
相關問題