简体   繁体   English

如何在 python 3 的 for 循环中修复此语法错误?

[英]How can I fix this Syntax error in a for loop in python 3?

Number = input("Type Your Factor number Here: ")
placeholder = int(Number)
a = 1
placeholder/a = x
for i in range(placeholder+1):
    if isinstance(x, int):
        print(placeholder/a)
    a += 1

SyntaxError: cannot assign to operator

it is giving me this error because I can not divide the"placeholder" variable by a.它给了我这个错误,因为我不能将“占位符”变量除以 a。

You're trying to assign a variable "x", which is not defined, to an operation "placeholder/a" which is incorrect.您正在尝试将未定义的变量“x”分配给不正确的操作“placeholder/a”。 We assign something to a variable, for example, placeholder = x.我们为变量赋值,例如 placeholder = x。

Another thing, you're dividing placeholder by a (a =1) so you can easily remove the division and make: placeholder = x.另一件事,您将占位符除以 (a =1),因此您可以轻松删除除法并生成:占位符 = x。

PS: Don't forget to define "x". PS:不要忘记定义“x”。

It works but I don't know what it is doing.它有效,但我不知道它在做什么。

Number = input("Type Your Factor number Here: ")
placeholder = int(Number)
a = 1
x= placeholder/a
print(x,'\n')
for i in range(placeholder+1):
    if isinstance(x, int):
       print(placeholder/a)
    a += 1

Just swap x position, you don't need placeholder alone.只需交换 x position,您不需要单独的占位符。

From your description of what the code should do in a recent comment, your code only needs a little reorganisation:根据您在最近的评论中对代码应该做什么的描述,您的代码只需要一点重组:

Number = input("Type Your Factor number Here: ")
placeholder = int(Number)
print(placeholder, '\n')
a = 1
for i in range(placeholder+1):
    x = placeholder / a
    if isinstance(x, int):
       print(x)
    a += 1

This is what you meant to write, however, I don't think the code will do what you want it to.这就是您要编写的内容,但是,我认为代码不会按照您的意愿进行操作。

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

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