2015-12-25 59 views
0

要求總價,稅款和小費。然後相互加入並給出總價。

from time import sleep 
def getprice(): 
    return input("What's the price of your bill?").lower() 

price = getprice() 

def gettax(): 
    return input("What's the restaurant's tax percent?").lower() 

tax = gettax() 

def gettip(): 
    return input("How much tip do you want to leave?") 

tip = gettip() 

percentage = float(tax)/100 
total= price*percentage + price + tip 

print(total) 

它給我和錯誤的總=行,我看了很多文章,但我無法修復它可以幫助我嗎?TypeError:無法乘以類型'float'錯誤的非整數序列我已經試過了一切

+0

你,你需要Corrs的變量轉換爲整數或浮點數。 –

回答

0

Python3 input函數返回一個字符串。

您試圖float * string + string + string

total= price*percentage + price + tip 

TypeError: unsupported operand type(s) for +: 'float' and 'str' 

您可以修復這個樣子,

from time import sleep 
def getprice(): 
    return float(input("What's the price of your bill?").lower()) 

price = getprice() 

def gettax(): 
    return float(input("What's the restaurant's tax percent?").lower()) 

tax = gettax() 

def gettip(): 
    return float(input("How much tip do you want to leave?")) 

tip = gettip() 

percentage = tax/100 
total= price*percentage + price + tip 

print(total) 
+1

非常感謝,我終於明白了我的錯誤。我很愚蠢,謝謝! – uniqxx

+0

不客氣 –

0

輸入讀取是一個字符串,它是一個序列,不能被float浮動。

int(price) 

解決它。

相關問題