0
hand = ['A','Q']
points = 0
player_cards1 = ['2']
value1 = 2
player_cards2 = ['3']
value2 = 3
player_cards3 = ['4']
value3 = 4
player_cards4 = ['5']
value4 = 5
player_cards5 = ['6']
value5 = 6
player_cards6 = ['7']
value6 = 7
player_cards7 = ['8']
value7 = 8
player_cards8 = ['9']
value8 = 9
player_cards9 = ['T']
value9 = 10
player_cards10 = ['Q']
value10 = 10
player_cards11 = ['K']
value11 = 10
player_cards12 = ['J']
value12 = 10
ACE11 = ['A']
value13 = 11

for hand in player_cards1:
    flag = True
    if flag == True:
        points = points + value1
    for hand in ACE11:
        flag = True
        if flag == True:
            points = points + value13
        for hand in player_cards2:
            flag = True
            if flag == True:
                points = points + value2
            for hand in player_cards3:
                flag = True
                if flag == True:
                    points = points + value3
                for hand in player_cards4:
                    flag = True
                    if flag == True:
                        points = points + value4
                    for hand in player_cards5:
                        flag = True
                        if flag == True:
                            points = points + value5
                        for hand in player_cards6:
                            flag = True
                            if flag == True:
                                points = points + value6
                            for hand in player_cards7:
                                flag = True
                                if flag == True:
                                    points = points + value7
                                for hand in player_cards8:
                                    flag = True
                                    if flag == True:
                                        points = points + value8
                                    for hand in player_cards9:
                                        flag = True
                                        if flag == True:
                                            points = points + value9
                                        for hand in player_cards10:
                                            flag = True
                                            if flag == True:
                                                points = points + value10
                                            for hand in player_cards11:
                                                flag = True
                                                if flag == True:
                                                    points = points + value11
                                                for hand in player_cards12:
                                                    flag = True
                                                    if flag == True:
                                                        points = points + value12
                                                    print points

It ran fine in the first five blocks and then it just gives me to total value of the whole deck (95). How do I fix this? It's supposed to give me the value of the cards in the Hand only. What did I do wrong in here?

4
  • 14
    OMG, you really need to refactor your code Commented Jul 31, 2012 at 23:09
  • 1
    It's properly formatted. It's just... uniquely written. Commented Jul 31, 2012 at 23:10
  • yes I'm pretty sure you guys find it amusing (not offended), I honestly didn't know how else to make it work because I tried other ways and it didn't work out so well for me. I know it should return a proper value but it doesn't Commented Jul 31, 2012 at 23:13
  • 3
    Really, I didn't found it amusing at all and I wasn't joking. I have a rule of thumb: whenever I found myself writing nested loops, if a reach the third it may be time for some refactoring. Perhaps you could explain the whole idea of what exactly are you trying to do. Commented Jul 31, 2012 at 23:19

2 Answers 2

5

Your line:

for hand in player_cards1:

Probably is not doing what you expect it to do. It looks like you think it's going to compare hand and player_cards1; instead what it does is create an iterator on player_cards1 that you access via the variable hand (meaning that hand is getting re-assigned, and will no longer be ['A', 'Q']). Here's a simpler example:

a = [1,2,3]
for item in a: //creates an iterator around a, with the variable item to refer to each list member
  print item

That three line program would output:

1
2
3

Instead, you probably want to iterate over the cards in the hand, e.g.:

for card in hand:
  //code here to look up the value of the card and add it to a running total

I'd also suggest rethinking the way you're tracking card values, as it's going to be very cumbersome . . . hint: there's only a few special cases where you can't use the card's face value.

Sign up to request clarification or add additional context in comments.

Comments

2

Edited to make the answer more didactical.

Use a dict to store the scores for each card.

card_points = { 'A': 11, '2': 2, ... }

Then you can iterate over each card in hard, look up its value in the dictionary, and sum the scores to give the total.

You might want to look at the sum function which could be useful here.

11 Comments

... you did something, that is completely not didactical, as it seemed like a homework for me and the poster should have been instructed on how to do it for him/herself rather then have it solved...
@MarkByers well, not from me. I wasn't joking at all
Well I'm sorry I'm no computer genius like some of you people I'm a Biochemistry major and this course is mandatory. You don't have to write out an answer for me just a suggestion on how to do it is good.
@Miroslav Hudak: I'm really bored, so I'm going to weight in because... well, because I can. Mark Byers' answer looks pretty didactic to me... There's not an specific implementation, just pointing what to do... The question's poster still has a lot of work to do there (hey, IMHO). Besides, this site is to ask/answer things, not necessarily to teach programming. I believe it's the course's professor's task to check whether his/her students actually know programming (dunno... by making questions about the assignment maybe)... Man, I'm bored!!
@Borrajax - you should check what his answer was before it was edited, which is when that non-didactic comment was made . . .
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.