Advertisement
mrScarlett

Python Global/Local

Nov 10th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. score=0 # score is outside of the stage 1 and stage 2 functions
  2.  
  3. def welcome():
  4.     name=input("Enter name: ")# NAME is a local variable it is only available in WELCOME function
  5.        
  6. def stage1():
  7.     global score # the global keyword makes the score available in the STAGE 1 function
  8.     answer=input("The keyword used to make outside variables available in a function").upper()
  9.     if answer=="GLOBAL":
  10.         print ("Correct")
  11.         score+=1
  12.     else:
  13.         print ("Wrong the answer is global")
  14.     stage2()
  15.  
  16. def stage2():
  17.     global score # the global keyword makes the score available in the STAGE 2 function
  18.     answer=input("'_____ Variable' The type of variable only available in a function").upper()
  19.     if answer=="LOCAL":
  20.         print ("Correct")
  21.         score+=1
  22.     else:
  23.         print ("Wrong the answer is LOCAL variable")
  24.    
  25. stage1()
  26. print (name) # this will not print as name is only available in the WELCOME function.
  27. print ("Your score is", score) # the score will be updated depending on the answers in stage 1 & 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement