TypeError:不能将序列乘以非int类型' float'错误我尝试了一切

时间:2015-12-25 21:35:50

标签: python python-3.x syntax

询问总价,税和小费。然后相互添加并给出总价。

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)

它给了我和总行=错误,我读了很多文章,但是我无法修复它可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

输入读取是一个字符串,即一个序列,不能由浮点数复制。

int(price)

解决它。

答案 1 :(得分: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)