Jump to content
  • Advertisement
Sign in to follow this  
Dalphin

unassinged in Python

This topic is 1543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
 

 

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

THANKS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!