0

I have created an simple, arbritrary function that prints out a value (1) based on what the first element from a randomly shuffled list is.

Looking at the code below: I am creating a variable and setting it to 1, then I'm a creating a small list (1,2,3) and then shuffling the list. The idea is, if the first element of that list (after shuffle) is 1 or 2, then print the value of 'correct', BUT if the first element of the list happens to be 3, set 'correct' to 0, and then run the function again. As you can see from the if statement, when 'value' is set to 0, I don't expect this to be printed at all because the function is being run again (chair()) before any printing takes place.

Having run this, I expected to find a print out of 5 values of 1. The problem is, I am seeing 0 values printed too. This seems like a very simple function, and I feel a little but stupid for not seeing why this is happenig. Any ideas?

def chair():

    correct = 1

    a = [1,2,3]
    random.shuffle(a)

    if a[0] == 3:
        correct = 0
        chair()               #run function again without printing

    print correct


for i in range(5):
    chair()
0

2 Answers 2

1

Just calling chair() does not modify correct. You need to do correct = chair(). The problem with that is chair() does not return anything; it prints it. You should move the printing to outside of the function and return instead:

def chair():

    correct = 1

    a = [1,2,3]
    random.shuffle(a)

    if a[0] == 3:
        correct = chair()

    return correct


for i in range(5):
    print chair()
Sign up to request clarification or add additional context in comments.

6 Comments

why can't you just return chair() instead of assigning it to correct
@danidee: No reason. I don't see a benefit either way.
@danidee Wouldn't that basically make correct superfluous? Would the value still be returned to the for loop?
yes it will...it could simply be removed instead of assigning chair() to it again for no reason
@danidee ah, you mean having two return statements? Replacing correct = chair() with return chair() and leaving return correct as it is? I thought you meant deleting return correct and only returning chair() from the function (and only if a[0] == 3)
|
0

Your function is recursive, so when you recall the function again to avoid printing, the function will still finish. If you added an else statement, you'd see those zeros disappear. Ex:

def chair():

    correct = 1

    a = [1,2,3]
    random.shuffle(a)

    if a[0] == 3:
        correct = 0
        chair()               #run function again without printing
    else:
        print correct


for i in range(5):
    chair()

edit: grammar edit: example

1 Comment

Perfect. That small change was exactly what I needed. Thank you.

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.