0

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?

1
  • 3
    You should move c = 0 outside the loop ;). Also, python supports += like in C. so you could do c += 1 instead of c = c + 1 Commented Aug 2, 2012 at 18:43

2 Answers 2

7

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")

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

Comments

1

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()

3 Comments

Thanks. Silly mistake on my part.
Uh-oh -- IndentationFault ... (sorry, some reason I thought that was funny because it sounds like SegmentationFault).
with the way SO determines code (by indenting) and the importance of indenting python code -- these things are bound to happen on a reasonably short timescale.

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.