我陷入了倍增变量的困境。 例如......
hrs = raw_input("Enter Hours:")
float(hrs)
rateperhr = raw_input("Enter rate:")
float(rateperhr)
grosspay = (hrs) * (rateperhr)
我得到的错误是:
Traceback (most recent call last):
File "hello.py", line 5, in <module>
grosspay = (hrs) * (rateperhr)
TypeError: can't multiply sequence by non-int of type 'str'
如何解决这个问题?
答案 0 :(得分:4)
我想你忘了:
hrs = float(hrs)
和
rateperhr = float(rateperhr)
无需括号:
grosspay = hrs * rateperhr
答案 1 :(得分:2)
您没有使用float()函数返回的值。根据{{3}},float()函数返回一个从其输入构造的浮点数,因此不会直接修改任何内容。
hrs = raw_input("Enter Hours:")
hrs = float(hrs)
rateperhr = raw_input("Enter rate:")
rateperhr = float(rateperhr)
grosspay = hrs * rateperhr
答案 2 :(得分:2)
它说:不能将序列乘以非整数类型&#39; str&#39; 因为您没有为浮点类型指定变量。如果你想改变变量类型,你必须写:
hrs = float(hrs)
您的代码写得很好:
hrs = float(raw_input("Enter Hours:"))
rateperhr = float(raw_input("Enter rate:"))
grosspay = (hrs) * (rateperhr)
print grosspay1
答案 3 :(得分:0)
有多种方法可以解决您的问题。除了上面提到的答案,你可以使用&#39;输入&#39;而不是raw_input或使用&#39; eval&#39;。
>>> hrs = input("Enter hrs: ")
Enter hrs: 10
>>> type(hrs)
<type 'int'>
>>> hrs = input("Enter hrs: ")
Enter hrs: 10.0
>>> type(hrs)
<type 'float'>
# Using eval
>>> hrs = eval(raw_input("Enter hrs: "))
>>> type(hrs)