[英]How do I fix this TypeError in Python concerning non-int and floats?
I'm working with Python 3 on Visual Studio Code and the project I'm working on is to calculate the user's lunar weight after receiving their terrestrial weight, then output how their weight changes overtime given an increase and a period of time.我正在Visual Studio Code上使用Python 3 ,我正在从事的项目是在接收到地球重量后计算用户的月球重量,然后输出他们的体重如何随着时间的增加和一段时间的变化而变化。
This is the error message I've received multiple times, no matter how many times I've re-code:这是我多次收到的错误消息,无论我重新编码多少次:
TypeError: can't multiply sequence by non-int of type 'float'
This specifically happens whenever I use the input() and/or sys.stdin.readline() functions, but all of the pertaining variables are integers or floats, whether or not I try to convert them to floats by using the float() function around my inputs.每当我使用 input() 和/或 sys.stdin.readline() 函数时都会发生这种情况,但所有相关变量都是整数或浮点数,无论我是否尝试使用 float() 函数将它们转换为浮点数围绕我的输入。
Here is my code:这是我的代码:
# this programs converts your terra weight to your lunar weight, then it takes increase over a period of time
# error whether I use float() or not
terraWeight = float(input("Terra Weight: "))
ask = input("Is this in LBS or KG? ")
# converts pounds to kilograms
# 2.204... is used to convert lbs to kgs
if ask == "lbs" or "LBS":
initial = terraWeight / 2.2046223302272
# didn't need this, but assignment error popped up
x = 0
# or replace input() with sys.stdin.readline()
increase = input("Increase: ")
period = input("Period: ")
# gets lunar weight over time
def lunarWeight():
global increase
global period
global x
global initial
print("Earth Weight = %s kgs." % terraWeight)
year = 0
lunarWeight = initial * 0.165
print("Moon Weight = %s kgs. \n" % lunarWeight)
postIncrease = lunarWeight * increase
for x in range(0, period):
year += 1
lunarWeight += postIncrease
print("Year %s = %s kgs." % (year, lunarWeight))
lunarWeight()
The terminal directs to the this section of my code:终端指向我代码的这一部分:
postIncrease = lunarWeight * increase
Which is on line 28. I'm not sure what the problem is, and I tried debugging but I still get the same error messages.第 28 行。我不确定问题是什么,我尝试了调试,但仍然收到相同的错误消息。 I saw other posts with the same problem but they had problems using lists, which I'm pretty sure I'm not using.我看到其他帖子有同样的问题,但他们在使用列表时遇到了问题,我很确定我没有使用。 Any advice please?请问有什么建议吗?
I think you should write this lines as:我认为你应该把这行写成:
increase = float(input("Increase: "))
period = int(input("Period: "))
period is used in range()
so it should be integer期间在range()
使用,因此它应该是整数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.