BBedit
BBedit

Reputation: 8057

Python thinks my List is a Tuple?

I have a list that is called step_segment. It should never be a tuple.

When I press "7" in my main program. I get:

Traceback (most recent call last):
  File "G:\programming\python\new_globals.py", line 205, in <module>
    main()
  File "G:\programming\python\new_globals.py", line 203, in main
    pick_random(STATS, step_segment, seen, master)
  File "G:\programming\python\new_globals.py", line 125, in pick_random
    step_segment, STATS = take_step(step_segment, STATS)
  File "G:\programming\python\new_globals.py", line 69, in take_step
    step_segment.append(STATS)
AttributeError: 'tuple' object has no attribute 'append'

The error only occurs when pick_random() is called:

def pick_random(STATS, seen, master):
    step_segment = []
    #if len(seen) >= 256:
    #   return seen, master
    while (len(step_segment)) < 128:
        step_segment, STATS = take_step(step_segment, STATS)
        if STATS[5] == "B":     # when there's a battle:
            randy = random.choice([0,1])
            if randy == 1:      # choose randomly between G and B
                step_segment = do_fight(step_segment, STATS)
            else:
                step_segment = do_glitch(step_segment, STATS)
            seen = seen + [STATS[0],STATS[5]]
    #if step_segment not in master:
        master.append(step_segment)
    time = get_frames(step_segment)
    print seen
    print time
    #return pick_random(STATS, seen, master)
    return seen, master

Full source: http://pastebin.com/fZgqtxZn

Upvotes: 1

Views: 1813

Answers (1)

NPE
NPE

Reputation: 500773

do_flight() returns a 2-tuple:

return step_segment, STATS

which you fail to unpack:

step_segment = do_fight(step_segment, STATS)

After this, step_segment becomes a tuple.

You probably meant to write

step_segment, STATS = do_fight(step_segment, STATS)

By way of general advice, you might want to keep your method signatures consistent to avoid this type of errors and/or learn a bit of object-oriented programming so that you don't have to keep passing the same variables everywhere.

Upvotes: 4

Related Questions