0
How do I make my var "w" work?
from random import randint x = randint(0,99) def my_fun(): if x = 5 w = 2 return w elif x > 5: w = 1 return w else: w = 3 return w def my_func(): my_fun() print(w) Could someone please tell me, how i can make this work?
3 Answers
+ 1
my_fun() returns a variable (because it has "return" statements). So you should assign a value to the calling statement:
myvar = my_fun()
Then you only have to print(myvar).
Please bear in mind that the w inside my_fun has nothing to do with the w inside my_func. Naming these two variables both as w will just confuse you.
Oh, and by the way, you missed a : after the first if.
+ 1
Thanks for your help, problem is solved:
from random import randint
x = randint(0,99)
def my_fun():
if x == 5:
w = 2
return w
elif x > 5:
w = 1
return w
else:
w = 3
return w
def my_func():
w = my_fun()
print(w)
Always remember:
= != == and w needed to be assigned thx
0
my_fun() returns a variable (because it has "return" statements). So you should assign a value to the calling statement:
myvar = my_fun()
Then you only have to print(myvar).
Please bear in mind that the w inside my_fun has nothing to do with the w inside my_func. Naming these two variables both as w will just confuse you.