简体   繁体   English

如何在 Python 中修复关于非整数和浮点数的这个 TypeError?

[英]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?请问有什么建议吗?

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期间在range()使用,因此它应该是整数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为公式修复“ TypeError:无法将序列乘以'float'类型的非整数”? - How do I fix “TypeError: can't multiply sequence by non-int of type 'float'” for my formulas? 如何修复“类型错误:不能将序列乘以非整数类型的‘浮点数’? - How do I fix 'Type Error: can't multiply sequence by non-int of type 'float'? 如何解决python中的“无法将序列乘以'float'类型的非整数”的问题 - How do I solve the issue “can't multiply sequence by non-int of type 'float'” in python 如何修复 TypeError:不能将序列乘以“numpy.float64”类型的非整数 - How to fix TypeError: can't multiply sequence by non-int of type 'numpy.float64' 如何在不出现TypeError的情况下对字符串进行乘数:不能将序列乘以'function'类型的非整数? - How do I multiply a string without getting TypeError: can't multiply sequence by non-int of type 'function'? 为什么我会得到 TypeError:不能将序列乘以“float”类型的非整数? - Why do I get TypeError: can't multiply sequence by non-int of type 'float'? 为什么会出现错误:“TypeError:无法将序列乘以‘float’类型的非整数”? - Why do I get the error: "TypeError: can't multiply sequence by non-int of type 'float'"? “当两个浮点列表相乘时,TypeError:不能将序列乘以'浮点'类型的非int” - “TypeError: can't multiply sequence by non-int of type 'float'” when multiplying two lists of floats Python非整数浮点数 - Python non-int float 为什么我得到错误不能将序列乘以'float'PYTHON类型的非int - Why do I get error can't multiply sequence by non-int of type 'float' PYTHON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM