DBWeinstein
DBWeinstein

Reputation: 9549

can't get python to do simple increment in for-loop

I'm just starting with Python (with a good amount of VBA under my belt) so I'm playing around with some simple syntax.

I've written this simple for-loop but the output seems wrong. I can't get the variable 'c' to increment.

Here is my code:

class Card:
    def county(self):
        for n in range(0,13):
            c = 0
            c = c + 1
            print c
    pick_card = Card()
    print pick_card.county()

and the output is just '1' printed 13 times followed by "None"

What am I doing wrong?

Upvotes: 0

Views: 302

Answers (2)

Maksim Skurydzin
Maksim Skurydzin

Reputation: 10541

You are assigning it 0 first and then increment it by 1. Thus it's always 1. Try using the following:

class Card:
    def county(self):
        c = 0
        for n in range(0,13):
            c += 1
            print c
    pick_card = Card()
    print pick_card.county()

Upvotes: 1

Wooble
Wooble

Reputation: 90027

Every time through the loop, you're setting c to 0, then adding 1, making it 1.

Also, your last line is printing the return value from your function, which doesn't return anything (hence the "None")

Upvotes: 7

Related Questions