-7

I am having a problem with my attack loop, when it runs it gets to the checkAtk function then restarts the direction loop.

I don't have a clue what's wrong with this code (and need to fix it before next Saturday). I welcome any advice or hint you might have.

import random
import time

#We define the intro function to introduce the player to the world
def displayIntro():
    # [...] content: calls to print() and sleep()

#Define a function to ask the player what direction they want to go
def chooseDir():
    direction = ''
    while direction != '1' and direction != '2' and direction != '3' and direction != '4':
        # [...] content: calls to print() and sleep()
        direction = input()
    return direction

#Define a function that check if the direction = a random int that the computer generates
def checkDir(direction, health, mana, money):
    # [...] content: calls to print() and sleep()
    friendlyDir = random.randint(1, 4)
    #If it does the player recieves 100 Rupees
    if direction == friendlyDir:
         # [...] content: calls to print() and sleep()
         health = 100
         mana = 100
         money = money + 100
    #if it dosent match we prepare for a fight
    else:
        # [...] content: calls to print() and sleep()

#define a function to ask the player to choose an attack
def chooseAtk(mana):
    chooseAtk = ''
    while chooseAtk != '1' and chooseAtk != '2' :
        # [...] content: calls to print() and sleep()
        #if players mana is > 0 they get a choice of a strength or a mana attack
        if mana > 0:
            # [...] content: calls to print() and sleep()
            chooseAtk = int(input())
        #if players mana < 0 the attack automatically goes to strength
        else:
            chooseAtk = 1
    return chooseAtk

#define a function to check the attack against Player Attack Power vs Monster Defense
def checkAtk(chooseAtk, health, mana, money):
    while chooseAtk == 1 and health > 0:
        if playerAp > monsterDef:
            # [...] content: calls to prin() and sleep()
            money = money + 100
        else:
            # [...] content: calls to print() and sleep()
            health = health - 10
    #if player chooses a mana based attack its Player Magic Power vs Monster Defense
    while chooseAtk == 2 and health > 0 and mana > 0:
        if playerMp > monsterDef:
            # [...] content: calls to print() and sleep()
            money = money + 100
            mana = mana - 10
        else:
            # [...] content: calls to print() and sleep()
            health = health - 10
            mana = mana - 10

#Set global variables
health = 100
mana = 100
money = 0
playerAp = random.randint(1,50)
playerMp = random.randint(1,50)
monsterDef = random.randint(1,50)

#Initiate the loop
displayIntro()

playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
    if health > 0:

        print('------------------------------')
        print('Health: '  + str(health))
        print('Mana: ' + str(mana))
        print('Rupees: ' + str(money))
        print('------------------------------')

        chosenDir = chooseDir()
        checkDir(chosenDir, health, mana, money)
        chooseAtk(mana)
        checkAtk(chooseAtk, health, mana, money)

while health == 0:
        print('Do you want to play again? (yes or no)')
        playAgain = input()   
Tonio
  • 414
  • 5
  • 17
  • Ok, I made a mistake with mi last comment. Edited it!! Sorry. – Raydel Miranda Mar 27 '14 at 13:12
  • Hi and Welcome! It appears as if you have received a school assignment or work task, and are asking us to solve a entire problem for you or simply find a library. Al tho we love a good challenge and try to help out in the best ways possible.. Some questions require you to show some sort of effort in solving this yourself first. It would there for be helpful if you could post a snippet of code or proof of research in what solutions you've tried and what worked/didn't work and post a stack-trace, output or just a description of what went wrong is sometimes enough. Can you show us what goes wrong – Torxed Mar 27 '14 at 13:12
  • @RaydelMiranda that has nothing to do with black hat, attack is a feature in his text adventure game. – bereal Mar 27 '14 at 13:13
  • @bereal You are right, my bad. – Raydel Miranda Mar 27 '14 at 13:16
  • when the user inputs their attack type it restarts the chooseAtk function meaning the out put looks like this – DarthzVayder Mar 27 '14 at 13:17
  • In which direction do you continue North(1), South(2), East(3) or West(4)? 1 You are walking briskly through the forest when you hear a creature cry out You increase your speed and hope he dosent find you..... You see a clearing coming up and decide to turn into it......... You come face to face with a creature you cannot identify You must fight. Will you use your Strength(1) or your Wisdom(2) to vanquish this foe 1 You come face to face with a creature you cannot identify You must fight. Will you use your Strength(1) or your Wisdom(2) to vanquish this foe – DarthzVayder Mar 27 '14 at 13:17
  • `def checkAtk(chooseAtk...)` naming a variable `chooseAtk` inside a function will override `def chooseAtk` for the record. – Torxed Mar 27 '14 at 13:17
  • Wow did you really copy the exact same output that is in your code, which we can already see and understand? By posting a "quote" that long you're just making things more confusing because there's no formatting involved in the comment section :P – Torxed Mar 27 '14 at 13:19
  • another problem i found is that i need a header basically the string that shows the players health mana and money dosen't update after an encounter – DarthzVayder Mar 27 '14 at 13:28
  • problem almost solved – DarthzVayder Mar 27 '14 at 14:52

2 Answers2

3

In this function call:

checkAtk(chooseAtk, health, mana, money)

The chooseAtk argument isn't going to work as expected, it's passing the function instead of the return value.

Consider this:

>>> def a():
    return 'a'

>>> def b(a):
    print a


>>> b(a)
<function a at 0x01E5C130>
>>> b(a())
a
atlasologist
  • 3,824
  • 1
  • 21
  • 35
2

I think this will work...

Here's where you went wrong:

1: You completely replaced function names with variables..

2: The return-value of checkMana was never used, you passed a function to checkAtk, see this code difference:

chooseAtk(mana)
checkAtk(chooseAtk, health, mana, money)

Vs working:

chosenAttack = chooseAtk(mana)
checkAtk(chosenAttack, health, mana, money)

3: The following code will never break because 1 != '1'.

def chooseAtk(mana):
    chooseAtkString = -1
    while chooseAtkString != 1 and chooseAtkString != 2 :
        print(' You must fight. ')
        if mana > 0:
            chooseAtkString = int(input())
        else:
            chooseAtkString = 1
    return chooseAtkString

4: The reason for an endless loop is not my fault even tho it sounds like I created that issue and I hate when people do that. This is your mess, not mine. I'm cleaning it up.

Here's why the loop occurs:

while AttackChosen == 1 and health > 0:
    if playerAp > monsterDef:
        money = money + 100
    else:
        health = health - 10

For the first if block, you don't loose any HP.. It's as simple as that.
So i did:

while AttackChosen == 1 and health > 0:
    if playerAp > monsterDef:
        money = money + 100
    health = health - 10

5: Why isn't mana/health updated? because...

Defining a function like this def checkAtk(AttackChosen, health, mana, money): will create local variables called health, mana, money instead of using the globals you've defined. Which means you will need to return these local variables back to the originating call which is:

checkAtk(chosenAttack, health, mana, money)

Try replacing that with:

health, mana, money = checkAtk(chosenAttack, health, mana, money)

and inside checkAtk do the following and the end:

return health, mana, money

Working code (For the love of the internets, next time post less code..)

import random
import time

#We define the intro function to introduce the player to the world
def displayIntro():
    print('You awake in a land like no other.')

#Define a function to ask the player what direction they want to go
def chooseDir():
    direction = ''
    while direction != '1' and direction != '2' and direction != '3' and direction != '4':
        print('In which direction do you continue North(1), South(2), East(3) or West(4)? ') 
        direction = input()
    return direction

#Define a function that check if the direction = a random int that the computer generates
def checkDir(direction, health, mana, money):
    print('You are walking briskly through the forest when you hear a creature cry out ')
    friendlyDir = random.randint(1, 4)
    #If it does the player recieves 100 Rupees
    if direction == friendlyDir:
         print('In the clearing there is a treasure chest and a natural spring')
         health = 100
         mana = 100
         money = money + 100
    #if it dosent match we prepare for a fight
    else:
        print('Dno what this does, but your code gave a ident syntax error because else: must be followed by something...')

#define a function to ask the player to choose an attack
def chooseAtk(mana):
    chooseAtkString = -1
    while chooseAtkString != 1 and chooseAtkString != 2 :
        print(' You come face to face with a creature you cannot identify ')
        if mana > 0:
            print( ' Will you use your Strength(1) or your Wisdom(2) to vanquish this foe ')
            chooseAtkString = int(input())
        else:
            chooseAtkString = 1
    return chooseAtkString

#define a function to check the attack against Player Attack Power vs Monster Defense
def checkAtk(AttackChosen, health, mana, money):
    while AttackChosen == 1 and health > 0:
        if playerAp > monsterDef:
            print(' The creature charges at you with his sword held high ')         
            money = money + 100
        else:
            print(' The creature charges at you with his sword held high ')
            print(' You lose 10 health ')
        health = health - 10
    #if player chooses a mana based attack its Player Magic Power vs Monster Defense
    while AttackChosen == 2 and health > 0 and mana > 0:
        if playerMp > monsterDef:
            print(' The creature charges at you with his sword held high ')
            money = money + 100
            mana = mana - 10
        else:
            print(' The creature charges at you with his sword held high ')
            health = health - 10
            mana = mana - 10
    return health, mana, money

#Set global variables
health = 100
mana = 100
money = 0
playerAp = random.randint(1,50)
playerMp = random.randint(1,50)
monsterDef = random.randint(1,50)

displayIntro()

playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
    if health > 0:

        print('------------------------------')
        print('Health: '  + str(health))
        print('Mana: ' + str(mana))
        print('Rupees: ' + str(money))
        print('------------------------------')

        chosenDir = chooseDir()

        checkDir(chosenDir, health, mana, money)

        chosenAttack = chooseAtk(mana)

        health, mana, money = checkAtk(chosenAttack, health, mana, money)

while health == 0:
        print('Do you want to play again? (yes or no)')
        playAgain = input()
user229044
  • 232,980
  • 40
  • 330
  • 338
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • tried it still restarting the chooseAtk function – DarthzVayder Mar 27 '14 at 13:24
  • @user3468656 Try it again -.- – Torxed Mar 27 '14 at 13:25
  • correction it spirals into an endless loop – DarthzVayder Mar 27 '14 at 13:26
  • @DarthzVayder Well you're not really showing any effort in solving stuff on your own, so meet the berserker known as Torxed The Mighty -.- There, fixed your endless loop as well (since you're obviously not interested in doing any problem-solving on your own.. Anything else we can assist you with when we're developing the entire game for you?) – Torxed Mar 27 '14 at 13:39
  • @DarthzVayder And yes, i removed a TON of output, sleeps and other stuff that's not really relevant to the CODE ISSUE.. We're not here to play games or wait for your content to load, we're here to solve programming issues so please strip your code of irrelevant information before posting.. Because you can add the dialogues later on. Thank you! – Torxed Mar 27 '14 at 13:40
  • @DarthzVayder Another thing to consider is the fact that you never modify `health` or `mana`, I don't know in which particular way you want to do this but you should consider doing it.. Or your player will live on forever :) – Torxed Mar 27 '14 at 13:46
  • @Torxed i know, like i said im a n00b programmer and need some advice. There is going to be a player status bar befort the chooseDir function to update the user as to amount of health, mana and money – DarthzVayder Mar 27 '14 at 13:48
  • @DarthzVayder You never really sad you were a noob programmer, all you sad was that you had a problem and it needed to be fixed before saturday, not that i care because we're all a noob at the beginning of all our journeys. Anyway, I think the code works somewhat better now, You choose a direction, you make a attack-type-choice, it gets executed, you'd supposedely loose HP/Mana (which you do, but only **within** the attack function because you don't update the global values) and you go back to "which direction...".. So i'd say it's working at this stage of your code. – Torxed Mar 27 '14 at 14:02
  • thank you for your help @Torxed but as far as updating the global values to display in my status bar im still trying to figure that out... :P – DarthzVayder Mar 27 '14 at 14:06
  • @DarthzVayder Solved that for you as well, check my #5 which is my latest edit. – Torxed Mar 27 '14 at 14:13
  • **When i try that i get an error:** The creature charges at you with his sword held high You lose 10 health Traceback (most recent call last): File "E:/School/Programming/fff.py", line 91, in health, mana, money = checkAtk(chosenAttack, health, mana, money) TypeError: 'NoneType' object is not iterable – DarthzVayder Mar 27 '14 at 14:32
  • @DarthzVayder Read my #5 again, you forgot to add something to `checkAtk`.. – Torxed Mar 27 '14 at 14:35
  • the 'return health, mana, money'? thats in there – DarthzVayder Mar 27 '14 at 14:37
  • @DarthzVayder change to `print(checkAtk(chosenAttack, health, mana, money))`, what's it say? because i could bet my trousers that you've missplaced the `return` statement :/ – Torxed Mar 27 '14 at 15:07
  • **This is the output of the Interpreter** The creature charges at you with his sword held high (90, 90, 0) so it is changing the variables but not outputting them in my status bar... – DarthzVayder Mar 27 '14 at 15:25
  • @DarthzVayder It works for me, at least Mana gets updated. For some reason the code gets stuck in another place.. which i will have to look later on if possible. – Torxed Mar 27 '14 at 15:42
  • @Torxed i would very much appreciate it. Im not the best at python been learning for about 1 1/2 months now and im still not getting the hang of it... :( – DarthzVayder Mar 27 '14 at 16:01