已分配值时出现属性错误
Getting an attribute error when the value is already assigned
当你到达程序打印 6 的部分后,它给了我下面提到的错误。即使正确归因的价值。我希望它在 Mario.x_location 值等于 LifeShroom.x 值时打印 6。然后,我希望它在按下 w 时将 Mario.x_location 的值增加一。输入 yes 而不是按 enter,然后输入 w 看看我的意思。我做错了什么?
start = input('say yes: ')
class Mario:
x_location = 4 #location of you
class LifeShroom:
x = 4 #location of the object.
if start == 'yes':
while start == 'yes':
command = input('') #press enter here, after you input yes.
if command == 'w':
Mario.x_location += 1 #change Mario.x_location
print(Mario.x_location,)
rules = [Mario.x_location == LifeShroom.x,]
if all(rules):
LifeShroom = True
if LifeShroom:
print(6) #type w again after it prints 6 and you will get the error below.
我得到的确切错误:
Traceback (most recent call last):
File "main.py", line 25, in <module>
rules_8 = [Mario.x_location == LifeShroom.x,
AttributeError: 'bool' object has no attribute 'x'
我可以说你在做一个无限循环。 while循环没有scape。
第一个 if 语句不是 运行第一个 if (if command == 'w':
),而是 运行 其他两个。在第一个中,它会破坏你的 class (LifeShroom = True
)。但它是 True,所以它将 运行 第三个。您的变量 LifeShroom 不是 class 而是布尔值 (True)。
下一个循环没有更多的 LifeShroom class,所以没有 LifeShroom.x class 属性。
当你到达程序打印 6 的部分后,它给了我下面提到的错误。即使正确归因的价值。我希望它在 Mario.x_location 值等于 LifeShroom.x 值时打印 6。然后,我希望它在按下 w 时将 Mario.x_location 的值增加一。输入 yes 而不是按 enter,然后输入 w 看看我的意思。我做错了什么?
start = input('say yes: ')
class Mario:
x_location = 4 #location of you
class LifeShroom:
x = 4 #location of the object.
if start == 'yes':
while start == 'yes':
command = input('') #press enter here, after you input yes.
if command == 'w':
Mario.x_location += 1 #change Mario.x_location
print(Mario.x_location,)
rules = [Mario.x_location == LifeShroom.x,]
if all(rules):
LifeShroom = True
if LifeShroom:
print(6) #type w again after it prints 6 and you will get the error below.
我得到的确切错误:
Traceback (most recent call last):
File "main.py", line 25, in <module>
rules_8 = [Mario.x_location == LifeShroom.x,
AttributeError: 'bool' object has no attribute 'x'
我可以说你在做一个无限循环。 while循环没有scape。
第一个 if 语句不是 运行第一个 if (if command == 'w':
),而是 运行 其他两个。在第一个中,它会破坏你的 class (LifeShroom = True
)。但它是 True,所以它将 运行 第三个。您的变量 LifeShroom 不是 class 而是布尔值 (True)。
下一个循环没有更多的 LifeShroom class,所以没有 LifeShroom.x class 属性。