[英]How do I fix “TypeError: can't multiply sequence by non-int of type 'float'” for my formulas?
[英]How do I fix this TypeError in Python concerning non-int and floats?
我正在Visual Studio Code上使用Python 3 ,我正在从事的项目是在接收到地球重量后计算用户的月球重量,然后输出他们的体重如何随着时间的增加和一段时间的变化而变化。
这是我多次收到的错误消息,无论我重新编码多少次:
TypeError: can't multiply sequence by non-int of type 'float'
每当我使用 input() 和/或 sys.stdin.readline() 函数时都会发生这种情况,但所有相关变量都是整数或浮点数,无论我是否尝试使用 float() 函数将它们转换为浮点数围绕我的输入。
这是我的代码:
# 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()
终端指向我代码的这一部分:
postIncrease = lunarWeight * increase
第 28 行。我不确定问题是什么,我尝试了调试,但仍然收到相同的错误消息。 我看到其他帖子有同样的问题,但他们在使用列表时遇到了问题,我很确定我没有使用。 请问有什么建议吗?
我认为你应该把这行写成:
increase = float(input("Increase: "))
period = int(input("Period: "))
期间在range()
使用,因此它应该是整数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.