如何在Python中解决与非整数和浮点数有关的TypeError?

问题描述 投票:0回答:1

我正在使用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行。我不确定是什么问题,我尝试调试,但仍然收到相同的错误消息。我看到了其他具有相同问题的帖子,但是他们在使用列表时遇到了问题,我敢肯定我没有使用过。有什么建议吗?

Screenshot

python python-3.x syntax
1个回答
0
投票
我认为您应该将此行写为:

increase = float(input("Increase: ")) period = int(input("Period: "))

© www.soinside.com 2019 - 2024. All rights reserved.