Reputation: 201
Why do I get a syntax error running this code? If I remove the highlighted section (return cards[i]) I get the error highlighting the function call instead.
Please help :)
def dealcards():
for i in range(len(cards)):
cards[i] = ''
for j in range(8):
cards[i] = cards[i].append(random.randint(0,9)
return cards[i]
print (dealcards())
Upvotes: 0
Views: 1698
Reputation: 76683
SyntaxError
is due to an unclosed paren after cards[i] = cards[i].append(random.randint(0,9)
AttributeError
when you call this function. You set cards[i]
to be a str
object then try to call append
on it. Strings don't have an append
method. cards
. This is usually a sign you're doing something wrong; it's more typical in Python simply to make a new list.
enumerate
.cards
. Using functions to mutate global state is a bad thing. There are two possibilities that would almost certainly be better:
deal_cards
which mutates some attribute self.cards
or whatever. (Probably the way to go.)cards
as an argument and returns a new list. (Probably not the way to go, but improves modularity, maintainability, and testability over your current technique.)Upvotes: 1
Reputation: 70158
cards[i] = cards[i].append(random.randint(0,9)
^
Missing closing parenthesis. And the return statement on the next line is incorrectly indented.
Upvotes: 5
Reputation: 798676
Missing a close:
cards[i] = cards[i].append(random.randint(0,9))
Upvotes: 1