2011-04-18 59 views
0

他們的問題是,使用功能,下面給出一個指令增加了三個項目,項目1,ITEM2和項目3的銷售稅,並將結果變量total_sales_tax營業稅蟒蛇

分配到目前爲止我有:

item1 = input(‘Enter price of the first item:’) 
item2 = input(‘Enter price of the second item:’) 
item3 = input(‘Enter price of the third item:’) 
return total_sales_tax = (item1 * .06) + (item2 * .06) + (item3 * .06) 

它運行不正常..我錯過了什麼嗎?

+2

當你說「它運行不正常」時,如果你給出* actual *錯誤信息或者錯誤的輸出結果,你會發現它非常有用。否則,我們只是猜測。 – 2011-04-18 23:42:55

回答

3

看起來你正在使用智能報價(‘’) - 你在Word中編寫代碼嗎?嘗試:

item1 = input('Enter price of the first item:') 
item2 = input('Enter price of the second item:') 
item3 = input('Enter price of the third item:') 
return total_sales_tax = (item1 * .06) + (item2 * .06) + (item3 * .06) 
0

首先,將輸入函數需要一個有效的Python表達式,並根據該Python文檔(http://docs.python.org/library/functions.html#input),不應該用於一般用戶輸入;應該使用raw_input。

此外,raw_input將返回一個字符串,當您嘗試將它乘以一個浮點數時(例如您的稅值),它會給您一個錯誤。在嘗試將其乘以之前,您應該將用戶輸入強制轉換爲浮點型。試試這個:

item1 = float(raw_input('Enter price of the first item:')) 
item2 = float(raw_input('Enter price of the second item:')) 
item3 = float(raw_input('Enter price of the third item:')) 
tax = 0.06 

total_sales_tax = item1 * tax + item2 * tax + item3 * tax 

print total_sales_tax