Advertisement

unassinged in Python

Started by June 30, 2014 05:39 PM
6 comments, last by Dalphin 10 years, 7 months ago

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

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

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)

Advertisement

I think you should explicitly declare your variable as global.


var = 0

def my_fun():
    global var
    var += 1

my_fun()
print var

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.

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

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

Advertisement

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.

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

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

THANKS

This topic is closed to new replies.

Advertisement