【发布时间】:2014-05-06 11:40:11
【问题描述】:
我的攻击循环有问题,当它运行时会到达checkAtk 函数,然后重新启动方向循环。
我不知道这段代码有什么问题(需要在下周六之前修复它)。我欢迎您提出任何建议或提示。
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()
【问题讨论】:
-
好的,我最后一条评论弄错了。编辑了!!对不起。
-
您好,欢迎您!好像您收到了学校作业或工作任务,并要求我们为您解决整个问题或只是查找图书馆。尽管我们喜欢一个很好的挑战,并尝试以最好的方式提供帮助。有些问题需要您首先自己解决这个问题。如果您可以发布代码的 sn-p 或研究证明,说明您尝试过哪些解决方案以及哪些有效/无效,并发布堆栈跟踪、输出或只是对发生了什么的描述,那将会很有帮助错误有时就足够了。你能告诉我们哪里出了问题
-
@RaydelMiranda 与黑帽无关,攻击是他的文字冒险游戏中的一个功能。
-
@bereal 你是对的,我的错。
-
当用户输入他们的攻击类型时,它会重新启动 chooseAtk 函数,这意味着输出看起来像这样
标签: python scope function-call