Variable value from function, doesn't transfer to the next function

In my code, the variable number_selected should be use in the function number_characteristic(number_selected), but its value is always equal to 0, and the value it get from number_random(), is not passed to the next function, I don’t get it why.

import random
import time
i=0
green = [0, "00"]
red = [9,30,7,32,5,34,3,36,1,27,25,12,19,18,21,16,23,14]
black = [28,26,11,20,17,22,15,24,13,10,29,8,31,6,33,4,35,2]
number_selected=0

def player_choice():
    choice=eval(input("Make your games...\n"))
    return choice

def number_random():
    number=red+black+green
    number_selected=random.choice(number)
    print ("nothing goes more...")
    for i in range (0,10):
        i += +1
        time.sleep(1)
        print("...")
    print ("games are made")
    print(number_selected)
    return number_selected

def number_characteristic(number_selected):
    print(number_selected)
    if number_selected in red:
        print("Red ...")
    if number_selected in black:
        print("Black ...")
    else:
        print("Green...")
    if number_selected==0 or number_selected==00:
        print (number_selected)
    else:
        if number_selected%2==0:
            print("Even...")
        else:
            print("Odd...")
            if number_selected>=1 and number_selected<=18:
                print("Misses...")
                if number_selected>19 and number_selected<=36:
                    print("Passes...")
                else:
                    print(number_selected)
                    return

def game(number_selected):
    player_choice()
    number_random()
    number_characteristic(number_selected)
    
game(number_selected)

            ```

here:

number_selected=random.choice(number)

number_selected does not update the global variable, instead this is a “new” variable which has a local scope (only exists in the function)

you do hand back the number_selected to the caller:

return number_selected

but then looking at the caller:

number_random()

you do nothing with the returned result

3 Likes

Thank you for your answer, but I don’t get it, I have already tied to put (number_selected) in the number_random() function as an argument, but I get the exact same result, my variable value remain to 0 ( the global variable is unchanged, has you said it)

2 Likes

what do you think this achieves?

then random_number function has a parameter, but the parameter also has a local scope (just like number_selected within the function body now)

so like you hopefully figured out, you need to hand back the random number, which you do:

return number_selected

but then, you do nothing with this returned randomly generated number:

// nothing happens with the returned result??
number_random()

Ok I understand what you meant, but not sure I did it the right way, so what I did, is that in the function number_selected(), before the return, I call the function number_characteristics(number_selected), now, it is working, and I think it is working because the function is call in the same function where the local variable is created, correct ?
Is it the good way to do it?

Not what you’re asking about, and you don’t necessarily need to do anything about it, I just want to point out that when code starts running off to the right side, when there’s a large number of different paths through it, then it should probably be expressed differently. I often don’t even try to read code like this, it’s too hard. I rewrite it first.

I was being a bit sloppy, I probably changed the meaning a bit by accident, but I think you can recognize your original idea in this, and it is MUCH easier to understand because there’s no nested logic in it.

def number_characteristic(number_selected):
    color = ("Red" if number_selected in red else
             "Black" if number_selected in black else
             "Green")

    if number_selected in [0, '00']:
        return f'{color}... 0'

    if number_selected % 2 == 0:
        return f'{color}... Even'

    if 0 < number_selected < 19:
        return f'{color}... Odd... Misses...'

    if 19 < number_selected < 37:
        return f'{color}... Odd... Passes...'

    return f'{color}... Odd... {number_selected}'

I’m even tempted to do…

def number_characteristic(number_selected):
    color = ("Red" if number_selected in red else
             "Black" if number_selected in black else
             "Green")
    return (
        f'{color}... 0' if number_selected in [0, '00'] else
        f'{color}... Even...' if number_selected % 2 == 0 else
        f'{color}... Odd... Misses...' if 0 < number_selected < 19 else
        f'{color}... Odd... Passes...' if 19 < number_selected < 37 else
        f'{color}... Odd... {number_selected}'
    )

But I admit this is weird, it’s coming from that I tend to prefer expressions over statements.

2 Likes

Thanks a lot for your suggestion, but I m not at this level of coding actually, the exercise is in the Games of Chance in Python learning course, and it’s only 23% of the learning, so may be in a few weeks, I may understand what it’s written, but today, its total chinese for me :slight_smile:

But you are.
The if-expressions are probably new to you, sorry about that. But returning the first “match” instead of nesting is definitely something you can already do.
Try to hug the left side! Avoid nesting, write it as series of steps, store results in variables.

Oh and f-strings. They are just formatting, nothing too crazy.
f'{python expression here} regular text {3 + 7} <- that\'s 10'

1 Like