using variables from function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

using variables from function

def yet(yut): yut="yaaah" return yut print(yut)uuu This code output is NameError: yut is not defined is there any way I can use variable yut outside function yet?

8th May 2018, 6:37 AM
sovietcat
sovietcat - avatar
6 Answers
+ 1
Still a newb but can’t you just declare it as a global variable?
8th May 2018, 6:53 AM
Ben Bryant
Ben Bryant - avatar
0
Ben Bryant I've never heard those I'll look it up thanks
8th May 2018, 7:23 AM
sovietcat
sovietcat - avatar
0
No prob. I think you just use the work ‘global’ before it. But they’re frowned upon for reasons I don’t yet understand 👍🏻
8th May 2018, 7:24 AM
Ben Bryant
Ben Bryant - avatar
0
you can have def yet(): and print(yet)
8th May 2018, 7:30 AM
Markus Kaleton
Markus Kaleton - avatar
0
Yes of course! Use the yet function as an output. This made my life a lot easier when trying to write a function that rolled a dice because I couldn’t get the dice roll out of the function and into the main code. Now I just assign the result of the func to a variable and call the variable
8th May 2018, 7:33 AM
Ben Bryant
Ben Bryant - avatar
0
I'll experiment with these suggestions thanks
8th May 2018, 8:18 AM
sovietcat
sovietcat - avatar