0

I got a bit stuck with a for loop - what I can see it is doing appears correct but isn't exactly what I'm trying to accomplish with it. I've come from a C background but any advice here would be beneficial.

def deal(player_num, cards):
    a = 0
    z = 0
    i = 0
    b = 0
    c = player_num
    hand = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]    
    for a in range(player_num):
        hand[a] = cards[i] + cards[i+b+c]
        b == b+1
        i == i+1
        z == z+1
    return hand 

So the for a in range(player_num) seems to be working (appends a++) but hand[0], hand[1], etc. gets the same hand. I guess it loops a but not the other variables, so I need to use more than 1 nested loop to get i++, b++ and c++?

2 Answers 2

6

b == b+1 is a logical expression (returning False every time), not an assignment. I'm guessing you want something like: b += 1

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

4 Comments

Since he wants b++, there isn't much debate, I'd say.
Ah perfect, thanks, so == doesn't assign, its only used to compare :)
Well ... there really is no equivalent to c's ++ operator in python since python integers are immutable. But yeah. I'm pretty sure this is what OP wants.
@user2127162 -- Precisely.
4

== is the equality operator in Python. = is the assignment operator.

== checks whether its left operand and its right operand are equal and return True or False accordingly. b and b+1 will never be equal to each other and either way it does not make sense to perform an operation without side-effect (like comparing two values for equality) and then do nothing with its result.

If you want to change the values of your variables, use the assignment operator = instead of ==.

1 Comment

I suppose you could create a class such that b == b + 1, but that certainly isn't the case for integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.