2017-03-07 74 views
-1

我試着寫在課堂上一個項目一個計算器,但保持擊中同一個錯誤:不能用'float'類型的非整型乘序列?

Traceback (most recent call last): File "C:\Users\shane\PythonPrograms\louie.py", line 56, in tax_percent = (price * tax) TypeError: can't multiply sequence by non-int of type 'float'

我的代碼如下。任何人都可以幫我調試嗎?

size_types = {'mini', 
       'regular', 
       'large' 
} 

meat_types = {'pork', 
       'beef', 
       'chicken' 
} 

def price_pork_meal(size): 
    if size == 'mini': 
     return 3.00 
    if size == 'regurlar': 
     return 4.00 
    if size == 'large': 
     return 6.00 
    else: 
     return (input('Please try again:')) 
    return size 

def price_beef_meal(size): 
    if size == 'mini': 
     return 4.00 
    if size == 'regular': 
     return 7.00 
    if size == 'large': 
     return 9.00 
    else: 
     return (input('Please try again:')) 
    return size 

def price_chicken_meal(size): 
    if size == 'mini': 
     return 3.50 
    if size == 'regular': 
     return 6.00 
    if size == 'large': 
     return 8.00 
    else: 
     return (input('Please try again:')) 
    return size 

def final_price(price, tax_percent): 
    price = size 
    total_price = price + tax_percent 
    return total_price 


size = (input('Please enter mini, regular, or large:\n')) 

price = size 

tax = 0.825 

tax_percent = (price * tax) 

print(final_price(price, tax_percent)) 
+0

請把您的完整代碼在你的問題 – River

+0

似乎變量'price'是一個'list'或者'tuple',你試圖用'float'乘以它。 – Abdou

回答

1

您的方法在python中無效。您不能像編寫錯誤那樣將序列與浮點值相乘。

這是行不通的:

a = (1,2,3) 
b = a * 0.25 

您可以使用numpy,支持這個(以及更多),或者自己實現這樣的:

a = (1,2,3) 
b = list(map(lambda x: x*0.2, a)) # or many other approaches; outer list needed in python3 to make it a list instead of an generator-expression 
相關問題