简体   繁体   中英

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.

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.


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. 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?

Screenshot

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM