0

I just want to start by saying i have only just started programing and am only interested in learning.

in a very simple turn-based fighting game, i give my player the choice to attack or defend but when i input a "d" for defend it will return the attack code.

import random
import time

play = 'y'

name = ' '

playerHp = 20
attack =  4
enemyHp = 0
enemyAttack = 0


def intro ():
    global name
    print('''
welcome to the scary forrest!
dangerous monsters live in this
area and you must destroy or
be destroyed! attack and defend
correctly and you may survive.''')
    time.sleep (2)

    print ('what is your name, adventurer?')

    name = input()

def randomEnemy ():
    x = random.randint (3,5)
    global enemyHp
    global enemyAttack
    enemyHp = x * 5
    enemyAttack = x
    print('''A goblin has jumped out to attack you, prepare to fight!''')


def battle ():
    global enemyHp
    global enemyAttack
    global playerHp
    global attack

    while enemyHp > 0 and playerHp > 0:
        print ('you have ' + str(playerHp) + ' health left')
        time.sleep (2)
        print ('the goblin has ' + str(enemyHp) + ' health left')
        time.sleep (2)
        print ('(a)ttack or (d)efend)

        choice = input()

        if choice == 'a' or 'attack':
            finalattack = attack + random.randint (-1,2)
            print ('you swing at the goblin and do ' + str(finalattack) + ' damage')
            time.sleep(2)
            print ('the goblin strikes you for ' + str(enemyAttack) + ' damage')
            playerHp = playerHp - enemyAttack
            enemyHp = enemyHp - finalattack
            choice = 0

        elif choice == 'd' or 'defend':
            print ('the goblin strikes at you!')
            print ('but you block his attack')
            heal = random.randint (5,6) - enemyAttack
            playerHp += heal
            print ('you heal for ' + str(heal) + ' Hp')
            choice =0

     if playerHp <= 0:
        print('you lose... noone finds your body because the \ngoblin drags you into  his cave')

    if enemyHp <= 0:
    print ('you stand victorious over your foe, but noone is \nreally impressed except yo\' momma')

intro()

randomEnemy()

battle()

is there a problem with my if statement?

if some one can help me and use small words that would be greatly appreciated.

  • looks like you're missing a ' – Embattled Swag Apr 02 '14 at 15:18
  • Your posted code seems to be missing some text; there is no `if` statement preceding the first `elif` clause, which seems related to the fact that the preceding `print` function call is missing a closing quote, at the very least. – chepner Apr 02 '14 at 15:19

2 Answers2

3

Yes, you need to test their choice with:

if choice == 'a' or choice == 'attack':

and:

elif choice == 'd' or choice == 'defend':

Your original statement:

elif choice == 'a' or 'attack':

Will always yield True as the string 'attack' is effectively True (not to mention it should be if and not elif; thanks to @chepner for spotting that).

(I haven't checked all your code).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • +1 The `if` itself is missing from his code, but it's likely it exhibits a similar problem as shown in the `elif`s. – chepner Apr 02 '14 at 15:20
0

The problem is that

elif choice == 'a' or 'attack':

is not doing what you think. Python's parser evaluates it as:

elif bool(choice == 'a') or bool('attack'):

and the boolean value of a non empty string is True. What you want is:

if choice == 'a' or choice == 'attack':

Note that I have replaced elif by a simple if, as it is the first one. (elif is short for else if).

Davidmh
  • 3,797
  • 18
  • 35