Example
The global Keyword
The global keyword allows modifying a global variable inside a function.
8 results
Example
The global keyword allows modifying a global variable inside a function.
Challenge
record_launch() adds 1 to a global counter and stops with an UnboundLocalError. Predict it, find out why Python treats the name as local, fix it by passing the count in and returning the new one, then…
Challenge
…the function, creating a local variable and leaving the global fuel unchanged. Predict the last line, compare both values, fix it by returning the total, then write a function that uses only its…
Example
Local variables exist only inside functions. Global ones are accessible everywhere.
Quiz
…quiz on Python variable scope: 15 questions on local and global variables, the global and nonlocal keywords, and nested functions.
Quiz
…int() and dynamic typing Constants by convention and the global keyword How it works 15 questions: 11 with one correct answer, 4 where you select every correct answer After each question you see the…
Article Python Fundamentals
…You'll find forum answers telling you to fix this with the global keyword. My advice is to skip that for now and do this instead: def burn(fuel, amount): return fuel - amount fuel = 100 fuel =…
Article Python Fundamentals
…def for lambda try True del from nonlocal while and elif global not with as else if or yield break except import pass Example of valid variable names in space context: orbital_velocity = 7.8 #…