简体   繁体   中英

AttributeError: 'str' has no attribute 'shape'

EDIT: I am very sorry to anyone who has been trying to figure this out... for SOME REASON in one of my other functions, I had a for loop

for turt in winningRowTurtles:
    winningRowTurtles[turt] = ''

I really don't know why... sorry for all the confusion!

I'm making a connect four game with Python, and I want to have four turtles to go to the winning tiles and flash, once a win is found. I've got this working... but it only works if as soon as the win is found, the turtles spawn in the middle of the screen and hide really quickly (which is distracting) and then go to their spots. I want to make it so they spawn and hide at the beginning of the game in the setup phase, NOT right at the end of the game. Here is my code:

winningRowTurtles = {}
for turt in range(4):
    winningRowTurtles[turt] = turtle.Turtle()
    winningRowTurtles[turt].up()
    winningRowTurtles[turt].ht()

This next code is what happens after the winner has been found:

if winnerFound == 1:
    for turt in winningRowTurtles:
        winningRowTurtles[turt].shape('circle')
        winningRowTurtles[turt].color('blue')
elif winnerFound == 2:
    for turt in winningRowTurtles:
        winningRowTurtles[turt].shape('square')
        winningRowTurtles[turt].color('red')

Then I get this error:

Traceback (most recent call last):
File "C:\[redacted]\Python\Connect Four!.py", line 353, in <module>
play_game()
File "C:\[redacted]\Python\Connect Four!.py", line 258, in play_game
winningRowTurtles[turt].shape('circle')
AttributeError: 'str' object has no attribute 'shape'

play_game() is the function where the second part of the code is run. Does anyone know why? This seems completely unreasonable. I defined winningRowTurtles[1,2,3,4] before I ran the other code... as turtles, not strings. Thanks in advance.

Consider:

for turt in range(4):

and:

I defined winningRowTurtles[1,2,3,4] before I ran the other code.

But range(4) is [0, 1, 2, 3]. What we would have expected you to say is:

I defined winningRowTurtles[0,1,2,3] before I ran the other code.

Using a dictionary for winningRowTurtles allows you to have what looks like an array that starts at 1 instead of 0 but makes it easier to introduce index errors and makes it easier for random strings to creep in. Ie the problem might not be one of the four turtles but an extra entry that's got into the dictionary since you do:

for turt in winningRowTurtles:

which assumes everything in the dictionary is a turtle.

I suggest, what a deleted comment did earlier, that you switch winningRowTurtles to an array to force consistent indexing and prevent accidental items from being inserted via indexing.

Something like:

winningRowTurtles = []

for _ in range(4):
    winningRowTurtles.append(turtle.Turtle())
    winningRowTurtles[-1].up()
    winningRowTurtles[-1].ht()

...

for turt in range(4):
    winningRowTurtles[turt].shape(winningRowTurtles[winnerFound].shape())
    winningRowTurtles[turt].color(winningRowTurtles[winnerFound].color())

Where winnerFound ranges from 0 - 3 also.

Yes, you'll have to rethink everything to be zero-based instead of one-based.

If you really want to stay with one-based indexes and a dictionary, make sure to adjust your range() appropriately:

for turt in range(1, 5):
    winningRowTurtles[turt] = turtle.Turtle()
    ...

Either way, you need to review your indexing to make sure it's consistent.

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