gpt4 book ai didi

python - 为什么我得到错误不能将序列乘以类型为 'float' 的非整数 PYTHON

转载 作者:太空宇宙 更新时间:2023-11-03 14:29:59 25 4
gpt4 key购买 nike

我写了一个简单的程序来计算一些电子零件的税,它是这样的:

print "How much does it cost?",    
price = raw_input()
print "Tax: %s" % (price * 0.25)
print "Price including tax: %s" % (price * 1.25)
raw_input ("Press ENTER to exit")

而且我一直收到这个错误:

Traceback (most recent call last):
File "moms.py", line 3, in <module>
print "Tax: %s" % (price * 0.25)
TypeError: can't multiply sequence by non-int of type 'float'

最佳答案

您需要先将 raw_input() 返回的字符串转换为 float:

price = float(raw_input("How much does it cost?")) # no need for extra print 

关于python - 为什么我得到错误不能将序列乘以类型为 'float' 的非整数 PYTHON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com