Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


unassinged in Python


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Dalphin   Members   -  Reputation: 193

Like
0Likes
Like

Posted 30 June 2014 - 11:39 AM

Hello everyone,

I'm making a small game for kids in python, but i have a problem.

my code is:

import random
import time
    
def geslaagd():
    print("goed gedaan")
    gevallenOpgelost = gevallenOpgelost +1

def gefaald():
    print("Helaas de patiënt is overleden")
    gevallenGefaald = gevallenGefaald +1
    
def game():
    print("Welkom, dokter, dit is de EHBO-afdeling van het ziekenhuis.")
    time.sleep(2)
    print("Vanaf nu is dit uw nieuwe werkplek en neemt u de beslissingen over de spoedgevallen.")
    time.sleep(2)
    print("Ik wens u succes!")
    time.sleep(2)
    print("En oja, mochten er mensen komen te overlijden door uw schuld, wordt u per direct ontslagen, dus verpest het niet!")
    time.sleep(2)

    gevallenOpgelost = 0
    gevallenGefaald = 0

    
    while gevallenGefaald == 0:
        situatie = random.choice (["gebroken arm", "gebroken been", 'hartaanval', "plotselinge bevalling", 'allergische reactie'])
        print ("Attentie, dokter, we hebben een spoedgeval. Er is iemand binnengekomen met een " + situatie)
        print("Wat gaat u doen? (kies uit: verbinden en een mitella eromheen (1), verbinden en gips eromheen (2), aan een monitor op de intensive-care (3), een kraamvrouw inroepen (4), een tegengif injecteren (5).)")

        if situatie == "gebroken arm":
            oplossingArm = input()
            oplossingArm = int(oplossingArm)
            if oplossingArm == 1:
                print("goed gedaan")
                gevallenOpgelost = gevallenOpgelost +1
        

            if oplossingArm != 1:
                print("Helaas de patient is overleden")
                gevallenGefaald = gevallenGefaald +1

        if situatie == "gebroken been":
            oplossingBeen = input()
            oplossingBeen = int(oplossingBeen)
            if oplossingBeen == 2:
                print("goed gedaan")
                gevallenOpgelost = gevallenOpgelost +1

            if oplossingBeen != 2:
                print("Helaas de patient is overleden.")
                gevallenGefaald = gevallenGefaald +1

        if situatie == 'hartaanval':
            oplossingHart = input()
            oplossingHart = int(oplossingHart)
            if oplossingHart == 3:
                    print("goed gedaan")
                    gevallenOpgelost = gevallenOpgelost +1

            if oplossingHart != 3:
                    print("Helaas de patient is overleden.")
                    gevallenGefaald = gevallenGefaald +1
    
        if situatie == "plotselinge bevalling":
            oplossingBevalling = input()
            oplossingBevalling = int(oplossingBevalling)
            if oplossingBevalling == 4:
                print("goed gedaan")
                gevallenOpgelost = gevallenOpgelost +1

            if oplossingBevalling != 4:
                print("Helaas de patient is overleden")
                gevallenGefaald = gevallenGefaald +1

        if situatie == "allergische reactie":
            oplossingAllergie = input()
            oplossingAllergie = int(oplossingAllergie)
            if oplossingAllergie == 5:
                geslaagd()

            if oplossingAllergie != 5:
                gefaald()
        
        

    gevallenOpgelost = str(gevallenOpgelost)
    print("Dokter, er zijn door uw schuld te veel patiënten overleden. U bent onslagen.")
    print("Het aantal patiënten dat u succesvol heeft geholpen is " +gevallenOpgelost)

opnieuw = 'ja'
while opnieuw == 'ja':

    game()

    print("Wil je opnieuw spelen (ja/nee)")
    opnieuw = input()

if opnieuw == 'nee':
    print("Bedankt voor het spelen van dit spel.")

(BTW i'm sorry it isn't english, but kids in my country have to understand it. So i hope you can still do something with it after you hear my explanation)

 

If i run this program, evrything works great, till i get to the read marked lines.

These are the first lines in which i use a defenition, but i geat this error:

 
Traceback (most recent call last):
  File "D:\game werkbank\python\EHBOtext-basedgameV1.0.py", line 95, in <module>
    game()
  File "D:\game werkbank\python\EHBOtext-basedgameV1.0.py", line 81, in game
    geslaagd()
  File "D:\game werkbank\python\EHBOtext-basedgameV1.0.py", line 7, in geslaagd
    gevallenOpgelost = gevallenOpgelost +1
UnboundLocalError: local variable 'gevallenOpgelost' referenced before assignment
 
I understand it's caused because i used it before i explained that the value has to be 0 at the beginning of the game and i tried to fix it, but i couldn't. So what is the way to explain it before using it,
 
I fully understand if you don't understand something of what i'm trying to say, if you ask it i can try to explain that part better.
Thanks
 

 



Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 2998

Like
0Likes
Like

Posted 30 June 2014 - 11:57 AM

Besides explicitly setting gevallenOpgelost = 0 at the top of the file, you may need to declare it global in the functions that use it.  (Don't forget to declare it global in game() either)

def geslaagd():
  global gevallenOpgelost 
  print("goed gedaan")
  gevallenOpgelost = gevallenOpgelost +1

Edited by BeerNutts, 30 June 2014 - 11:58 AM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#3 Lysy   Members   -  Reputation: 583

Like
0Likes
Like

Posted 30 June 2014 - 11:58 AM

I think you should explicitly declare your variable as global.

var = 0

def my_fun():
    global var
    var += 1

my_fun()
print var

Edited by Lysy, 30 June 2014 - 01:46 PM.


#4 Dalphin   Members   -  Reputation: 193

Like
0Likes
Like

Posted 30 June 2014 - 12:49 PM

Thanks for the reactions, but i still don't understand where i have to change it then in game(). Sorry BTW, but i'm learning python now and this is just to try something to understand how it works.



#5 Lysy   Members   -  Reputation: 583

Like
0Likes
Like

Posted 30 June 2014 - 01:44 PM

Put this two:

gevallenOpgelost = 0
gevallenGefaald = 0

before any function (i.e just after imports), then in every function you have, in the first line of each of them, write:

global gevallenOpgelost
global gevallenGefaald

This should to the trick



#6 Dalphin   Members   -  Reputation: 193

Like
0Likes
Like

Posted 30 June 2014 - 01:50 PM

Yeah it worked. many thanks for your help. I finally know how to do it now.



#7 Code Fox   Members   -  Reputation: 1804

Like
1Likes
Like

Posted 30 June 2014 - 08:09 PM

Just a couple little things I noted in the code ( not nit-picking )

 

 1: This kind of code can lead to program crashes when dealing with user inputs and explicitly converting variable types

            oplossingArm = input()
            oplossingArm = int(oplossingArm)

When dealing with user input, it is always a good idea to use exception handling in case of incorrect input [ LINK ]

 

2: To save some typing, you can use "+=" in cases like this

gevallenOpgelost = gevallenOpgelost +1 // gevallenOpgelost += 1 is faster to type

3: If you are going to use a variable globally it should be initialized globally, instead of inside of a function ( definition ). This saves a lot of confusion ( and unexpected execution ) as the case of this post.


Does Anyone Actually Read This ?
 


#8 Dalphin   Members   -  Reputation: 193

Like
0Likes
Like

Posted 01 July 2014 - 04:33 AM

okay thanks, i don't understand it completely, but that will come after i learn some more.

THANKS






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS