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?

13th Apr 2017, 2:00 PM
Sascha Marc Stankiewicz
Sascha Marc Stankiewicz - avatar
3 Réponses
+ 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.
13th Apr 2017, 2:57 PM
Álvaro
+ 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
13th Apr 2017, 3:11 PM
Sascha Marc Stankiewicz
Sascha Marc Stankiewicz - avatar
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.
16th Apr 2017, 6:26 PM
OHM PATEL