Global variables in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Global variables in Python

Do i have to define variables as global if i want to use them Outside a function? Why is Not Every Variable global by default? For Saving Memory Space? In my example Code do i have to Return the number ‚Return numberdrawn‘ or is it accessable without returning it. So i Could Return True or False but still could use the Number Outside the Function? https://code.sololearn.com/c6i06EVv6mFS/?ref=app

5th Jul 2018, 8:37 PM
Daniel
Daniel - avatar
11 Answers
+ 5
Daniel, if you're asking how you can use variables defined inside a function outside of that function, then the answer is you can't. Variables defined inside a function are "local" which means they can only be "seen" inside the function. Variables are not global unless you define them outside any function. This prevents confusion if there are a lot of variables in your program. In your code, you have done a number of things which are unnecessary. Your randomnumber() function can be deleted entirely because its only purpose is to return random.randint, which is already a function! So you can replace your randomnumber() function with: numberdrawn = random.randint(0, 36) To use your numberdrawn in your match() function, you do not have to declare it as a global variable. Instead you should pass it as a parameter in the brackets like this: def match(numberdrawn): You should check out the python tutorials on Sololearn to find out about parameters in functions and local/global variables. Hope this helped.
16th Jul 2018, 9:02 PM
Lachlan
+ 4
your program is "terrible" within your function you declare a variable as global which is not existing global. so you define a global variable. globals as TurtleShell said are not good. but ....well... globals as you use are even worse.... but very interesting. Here is the essence of what you did. https://code.sololearn.com/cB5W8WsSvrV0/?ref=app but...really .... very interesting. clear upvote to your post (-:
5th Jul 2018, 9:12 PM
Oma Falk
Oma Falk - avatar
+ 4
numberdrawn = match() wäre der erste Schritt. versuch das mal. match sollte nur die Nummer ziehen. Die Auswertung kommt im Hauptprogramm. oder un einer anderen Funktion. Golden Rule: every function has exactly one task.
6th Jul 2018, 8:31 AM
Oma Falk
Oma Falk - avatar
+ 3
Global variables most of the time are not a good idea, that’s because anything can change it. If you use return and parameters properly you wont have the need to add global variables in most cases.
5th Jul 2018, 8:57 PM
TurtleShell
TurtleShell - avatar
+ 3
the other way is true global only makes sense within a function x = 1 def add1(): global x x+=1 add1() print(x) output is 2.
5th Jul 2018, 9:00 PM
Oma Falk
Oma Falk - avatar
+ 2
Daniel wird schon! Für einen Anfänger ist das schon nicht ganz trivial.
6th Jul 2018, 9:53 AM
Oma Falk
Oma Falk - avatar
+ 1
thank you 😄
6th Jul 2018, 4:48 AM
Daniel
Daniel - avatar
+ 1
das klingt schon mal nach einem sehr guten tipp. dann werde ich das ganze mal von vorne neu aufbauen. vielen dank. 👍
6th Jul 2018, 9:44 AM
Daniel
Daniel - avatar
+ 1
und für eine oma kennst du dich ganz schön gut aus mit programmieren 😀😃😀👍👏
6th Jul 2018, 10:05 AM
Daniel
Daniel - avatar
0
ok what can i do to make chips + 7 if it is in smallWin and chips + 24 if it is in bigWin and how can i double the win if the same nunber is hit again. sorry i‘m a beginner... https://code.sololearn.com/cN871sJvQVa9/?ref=app
6th Jul 2018, 8:14 AM
Daniel
Daniel - avatar
0
Η ΑΛΗΘΕΙΑ ΕΙΝΑΙ ΟΤΙ Η ΠΑΙΘΟΝ ΕΙΝΑΙ ΔΥΣΚΟΛΗ ΓΛΩΣΣΑ ΠΡΟΓΓΡΑΜΑΤΙΣΜΟΥ ΓΙΑΤΙ ΕΚΑΝΑ ΛΑΘΟΣ ΣΤΟ Ποια είναι η έξοδος αυτού του κώδικα; >>> spam = 2 >>> αυγά = 3 >>> del spam >>> αυγά = 4 >>> spam = 5 >>> εκτύπωση (spam * αυγά) ΠΟΙΑ ΕΙΝΑΙ Η ΑΠΑΝΤΗΣΗ
6th Aug 2020, 5:46 PM
Chris
Chris - avatar