Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global vs local variable
#1
Hi I'm a teacher and I just began learning Python so I can show some tips to my students. I code a little math game and I have an issue with global vs local variable.

I'm trying to define 2 games and launch the first one and the second on a condition (bonne == 3), but I don't know how to do this.

(I'm a french speaking teacher, so sorry for my poor english)
Thank you


import random
import time
print ('Bienvenue')

def GAME() :
  bonne = 0
  mauvaise = 0
  jeu = 0
  while True :
    a= int(random.randint (0,10))
    b = int(random.randint (0,10))
    reponse = a+b
    
    print (str(a) + '+' + str(b) + '=' + '?')
    joueur = input ('Quelle est la bonne réponse ?')
    time.sleep (0.5)
    
    if int(joueur) == reponse :
      print ('Bravo')
      bonne = bonne + 1
      time.sleep(0.3)
      print ('Tu as ' + str(bonne) + ' bonnes réponses et ' + str(mauvaise) + ' mauvaises réponses') 
    
    else :
      print ('VRAIMENT !!!')
      mauvaise = mauvaise + 1
      time.sleep(0.3)
      print ('Tu as ' + str(bonne) + ' bonnes réponses et ' + str(mauvaise) + ' mauvaises réponses') 
    
    if bonne == 3 :
      print ('Tu as complété le jeu avec ' + str(mauvaise) + ' mauvaise réponse')
      jeu = jeu + 1
      break
    
    if mauvaise == 3 :
      print ('Trop de mauvaises réponses')
      time.sleep (0.5)
      print ('GAME OVER')
      break

def GAME2(): << def of GAME 2
  
  
if jeu == 0 : << play the fisrt game
  GAME()

if jeu == 1 : << play the second game if you succeed the first one
  GAME2()
Reply


Messages In This Thread
Global vs local variable - by mquesnel - Jan-12-2018, 03:01 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 03:19 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 04:15 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 06:47 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 07:13 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 07:33 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 08:31 PM
RE: Global vs local variable - by mpd - Jan-12-2018, 10:59 PM
RE: Global vs local variable - by Larz60+ - Jan-12-2018, 10:45 PM
RE: Global vs local variable - by Gribouillis - Jan-12-2018, 11:49 PM
RE: Global vs local variable - by mpd - Jan-13-2018, 12:21 AM
RE: Global vs local variable - by mquesnel - Jan-13-2018, 06:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,205 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,823 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,046 Jan-27-2023, 05:32 PM
Last Post: caslor
  How to use global value or local value sabuzaki 4 1,178 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,931 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Global vs. Local Variables Davy_Jones_XIV 4 2,685 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Variable scope - "global x" didn't work... ptrivino 5 3,086 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Global - local variables Motorhomer14 11 4,291 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,247 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  from global space to local space Skaperen 4 2,345 Sep-08-2020, 04:59 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020