简体   繁体   English

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

[英]Why do I get error can't multiply sequence by non-int of type 'float' PYTHON

I wrote a simple program to calculate tax for some electrical parts, it goes like this: 我写了一个简单的程序来计算某些电气部件的税,它是这样的:

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")

And I keep getting this error: 我一直收到这个错误:

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 

This means that price is not a number. 这意味着price不是数字。 In fact, it's a string, since that's what raw_input returns. 实际上,它是一个字符串,因为这是raw_input返回的内容。 You'll want to parse it using float , or use input instead of raw_input . 您将要使用float解析它,或使用input而不是raw_input

基本上你不能将一个字符串乘以浮点数,也许你想要的是

price = float(raw_input())

The price is a string. price是一个字符串。 You need to create a float from the string you have input: 您需要从输入的字符串创建一个浮点数:

>>> price_str = raw_input()
123.234
>>> print type(price)
<type 'str'>
>>> price = float(price_str)
>>> print type(price)
<type 'float'>
>>> print "Tax: %s" % (price * 0.25)   
Tax: 30.8085
>>> 

暂无
暂无

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

相关问题 为什么我会收到“不能将序列乘以‘float’类型的非整数”错误? - Why do I get "can't multiply sequence by non-int of type 'float'" error? 为什么会出现错误:“TypeError:无法将序列乘以‘float’类型的非整数”? - Why do I get the error: "TypeError: can't multiply sequence by non-int of type 'float'"? 为什么我会得到 TypeError:不能将序列乘以“float”类型的非整数? - Why do I get TypeError: can't multiply sequence by non-int of type 'float'? 如何修复“类型错误:不能将序列乘以非整数类型的‘浮点数’? - How do I fix 'Type Error: can't multiply sequence by non-int of type 'float'? 为什么我得到 TypeError:不能将序列乘以“float”类型的非整数 - Why I get TypeError: can't multiply sequence by non-int of type 'float' 如何解决python中的“无法将序列乘以&#39;float&#39;类型的非整数”的问题 - How do I solve the issue “can't multiply sequence by non-int of type 'float'” in python 不能通过Non-Int类型浮点乘以序列? 蟒蛇 - Can't Multiply Sequence By Non-Int of Type Float? Python 不能将序列乘以&#39;float&#39;Python类型的非整数 - can't multiply sequence by non-int of type 'float' Python 不能在Python中将序列乘以&#39;float&#39;类型的非int - Can't multiply sequence by non-int of type 'float' in Python Python不能将序列乘以&#39;float&#39;类型的非int - Python can't multiply sequence by non-int of type 'float'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM