Python Forum
Please help me with this program
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me with this program
#1
Whenever i run the program it continually loops. Please help!
import random
#Misc. variables
y = 0
monNum = random.randint(1, 2)

#Player variables
player1HP = 10
gold = 0

#Enemy HP
goombaHP = 10
slimeHP = 15

#Monster Number Generator
def monNumCmd():
    monNum = random.randint(1,1)

#Attack command
def attack():
    if(monNum==1):
        global goombaHP
        attackValue = random.randint(1, 10)
        goombaHP = goombaHP - attackValue
        print("Goomba HP:")
        print(goombaHP)
    elif(monNum==2):
        global slimeHP
        attackValue = random.randint(1,10)
        slimeHP = slimeHP - attackValue
        print("Slime HP:")
        print(slimeHP)

#Enemy attack
def enemyAttack():
    if(monNum==1):
        global player1HP
        enemyAttack = random.randint(1, 3)
        player1HP = player1HP - enemyAttack
        print("Player HP:")
        print(player1HP)

#Inventory Screen
def inventory():
    print("Player HP:")
    print(player1HP)
    print()
    print("Gold:")
    print(gold)
    print()

def move():
    y=y-1

#Battle input chain
def enemyBattleChain():
    global y
    global gold
    global monNum
    if(monNum==1):
        if(player1HP<=0):
            print("You have been defeated!")
            y=y+1
        elif(goombaHP<=0):
            print("You have slain the Goomba!")
            gold = gold + random.randint(1,3)
            inventory()
        else:
            print("A goomba has crossed your path!")
            print()
            command = input("What will you do?:")
            print()
            if(command == "help"):
                print("attack")
                
                print()
            elif(command == "attack"):
                attack()
                enemyAttack()
            else:
                print("Command not found")
    elif(monNum==2):
        if(player1HP<=0):
            print("You have been defeated!")
        if(slimeHP<=0):
            print("You have slain the Slime!")
        else:
            print("A slime has crossed your path!")
            print()
            command = input("What will you do?:")
            print()
            if(command=="help"):
                print("attack")
                print()
            elif(command == "attack"):
                attack()
            else:
                print("Command not found")


while(y==0):
    enemyBattleChain()
Reply
#2
Quote:
while(y==0):
    enemyBattleChain()

...you never change "y", so of coarse it'll run forever. Perhaps you wanted to return y somewhere, and set it within the loop? Like y = enemyBattleChain()?
Reply
#3
Help us out.
Where do you think the loop is occurring?
There must be one of two things happening (because I don't see any while or for loops)
There is either some recursion, or you are calling a function that calls the calling function,
and will continue to do so until the cows come home
Reply
#4
If you slay a creature, y is not changed. The loop only ends when y is not 0, so you have to change it to end the loop.

There are other problems with your program, too. This is not a good situation to use global. Good situations are so incredibly rare, that as a beginner you should just not use global. Use parameters to pass values to functions, and use return statements to get values out of functions. Also, you are doing the exact same thing with slime molds and goombas. The monNum variable should just be used to set the name and HP of the monster, and then just deal with the monster using those attributes.

You should really check out the text adventure tutorial. And use code tags.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Ok I see the loop now!
Reply


Forum Jump:

User Panel Messages

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