+ 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
11 Antworten
+ 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.
+ 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 (-:
+ 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.
+ 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.
+ 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.
+ 2
Daniel wird schon!
Für einen Anfänger ist das schon nicht ganz trivial.
+ 1
thank you 😄
+ 1
das klingt schon mal nach einem sehr guten tipp. dann werde ich das ganze mal von vorne neu aufbauen. vielen dank. 👍
+ 1
und für eine oma kennst du dich ganz schön gut aus mit programmieren 😀😃😀👍👏
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
0
Η ΑΛΗΘΕΙΑ ΕΙΝΑΙ ΟΤΙ Η ΠΑΙΘΟΝ ΕΙΝΑΙ ΔΥΣΚΟΛΗ ΓΛΩΣΣΑ ΠΡΟΓΓΡΑΜΑΤΙΣΜΟΥ
ΓΙΑΤΙ ΕΚΑΝΑ ΛΑΘΟΣ ΣΤΟ Ποια είναι η έξοδος αυτού του κώδικα;
>>> spam = 2
>>> αυγά = 3
>>> del spam
>>> αυγά = 4
>>> spam = 5
>>> εκτύπωση (spam * αυγά)
ΠΟΙΑ ΕΙΝΑΙ Η ΑΠΑΝΤΗΣΗ