繁体   English   中英

TypeError:无法将序列乘以“ float”类型的非整数错误我已经尝试了一切

[英]TypeError: can't multiply sequence by non-int of type 'float' error I've tried everything

询问总价,税金和小费。 然后彼此相加,得出总价。

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)

它给了我并在total =行上出错,我读了许多文章,但我无法解决,有人可以帮助我吗?

读取的输入是一个字符串,即一个序列,不能与浮点数相乘。

int(price)

解决它。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM