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.