【发布时间】:2014-05-13 23:57:27
【问题描述】:
我只想说我才刚刚开始编程,只对学习感兴趣。
在一个非常简单的回合制格斗游戏中,我让玩家选择攻击或防御,但是当我输入“d”表示防御时,它会返回攻击代码。
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()
我的 if 语句有问题吗?
如果有人可以帮助我并使用小词,将不胜感激。
【问题讨论】:
-
你好像少了一个'
-
您发布的代码似乎缺少一些文字;第一个
elif子句之前没有if语句,这似乎与前面的print函数调用至少缺少结束引号有关。
标签: if-statement python-3.x while-loop