简体   繁体   English

无法找出TypeError

[英]Can't figure out TypeError

I got a (TypeError: can't multiply sequence by non-int of type 'float') error and I can't figure out how to fix it. 我遇到了一个(TypeError:无法将序列乘以'float'类型的非整数)的错误,我不知道如何解决它。 I've searched the error and wasn't able to figure out how to fix it because the code examples that had the error didn't look like the code I'm having a problem with. 我已经搜索了该错误,但无法找出解决方法,因为出现错误的代码示例与我遇到问题的代码并不相似。

# Variable stuff
initialMagnitudes = input("What are the initial magnitudes of the 
vector? (separate with spaces)\t").split()
for string in initialMagnitudes:
    string = float(string)
initialDirections = input("What are the initial directions of the 
vector? (in degrees and separate with spaces)\t").split()
for string in initialDirections:
    string = float(string)
vector_i = []
vector_j = []
vectorEquation = "Xi + Yj"
resultantMagnitude = 0.0
resultantDirection = 0.0
resultantVectorEquation = "R = Xi + Yj"


def find_vector_equation():
    global vectorEquation
    global vector_i
    global vector_j
    for magnitude in initialMagnitudes:
        theta = 
float(initialDirections[initialMagnitudes.index(magnitude)])
        vector_i.append(math.cos(math.radians(theta))*magnitude)
        vector_j.append(math.sin(math.radians(theta))*magnitude)
        vectorEquation = "{}i + {}j".format(vector_i, vector_j)
    print("The resultant vector equation is: "+vectorEquation)
find_vector_equation()

Here is all the code that could be part of the problem. 这是可能是问题一部分的所有代码。 Thanks in advance for all the help! 在此先感谢您提供的所有帮助!

This is the problem: 这就是问题:

for string in initialMagnitudes:
    string = float(string)

This doesn't change anything in initialMagnitudes . 这不会改变initialMagnitudes任何initialMagnitudes Instead, you change the variable string to be a float version of its string value, and then throw away that float. 而是将变量string更改为其字符串值的浮点版本,然后丢弃该浮点。 The loop variable in a for statement is a value, not a slot in the list; for语句中的循环变量是一个值,而不是列表中的插槽。 it has no idea where it came from and assigning to it does not put the value back in the list. 它不知道它来自哪里,分配给它也不会将该值放回列表中。

Since the items of initialMagnitudes are still strings, trying to multiply by one of them is trying to multiply a string by a float, and you get the error you get. 由于initialMagnitudes的项目仍然是字符串,因此尝试将它们乘以其中之一就是将字符串乘以浮点数,就会得到错误。

Best to do it with a list comprehension: 最好通过列表理解来做到这一点:

initialMagnitudes = [float(value) for value in initialMagnitudes]

暂无
暂无

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

相关问题 TypeError:“ int”对象不可下标无法解决此问题 - TypeError: 'int' object is not subscriptable Can't figure this out 我不知道如何修复 TypeError: can't concat str to bytes mean - I can't figure out how to fix TypeError: can't concat str to bytes mean 错误? 无法弄清楚 - Error? Can’t Figure Out 不知道这个卡塔 - Can't figure out this Kata 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 我不知道为什么我得到这个:TypeError:'builtin_function_or_method'对象不可下标 - I can't figure out why I get this: TypeError: 'builtin_function_or_method' object is not subscriptable Python 不断为列表抛出“TypeError”错误,我不知道如何修复它 - Python keeps throwing a "TypeError" error for a list, and I can't figure out how to fix it 无法弄清楚TypeError:__ init __()需要3个参数(给定2个) - Can't figure out TypeError: __init__() takes exactly 3 arguments (2 given) 获取TypeError:列表索引必须是整数,而不是元组,无法弄清原因 - Getting TypeError: list indices must be integers, not tuple, can't figure out why TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例,我不知道为什么 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' and I can't figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM