我的攻击循环出现问题,当它运行时会进入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()
答案 0 :(得分:3)
在此函数调用中:
checkAtk(chooseAtk, health, mana, money)
chooseAtk
参数不会按预期工作,它传递函数而不是返回值。
考虑一下:
>>> def a():
return 'a'
>>> def b(a):
print a
>>> b(a)
<function a at 0x01E5C130>
>>> b(a())
a
答案 1 :(得分:2)
我认为这会奏效......
1:您完全用变量替换了函数名称。
2:checkMana的返回值从未使用过,你将函数传递给checkAtk,看到这段代码差异:
chooseAtk(mana)
checkAtk(chooseAtk, health, mana, money)
与工作:
chosenAttack = chooseAtk(mana)
checkAtk(chosenAttack, health, mana, money)
3:以下代码永远不会中断,因为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:无限循环的原因不是我的错,即使这听起来像是我创造了这个问题而且我讨厌人们这样做。这是你的烂摊子,不是我的。我正在清理它。
这就是循环发生的原因:
while AttackChosen == 1 and health > 0:
if playerAp > monsterDef:
money = money + 100
else:
health = health - 10
对于第一个if
区块,您不会松开任何HP ..它就是这么简单。
所以我做了:
while AttackChosen == 1 and health > 0:
if playerAp > monsterDef:
money = money + 100
health = health - 10
5:为什么没有法术力/健康状况更新?因为...
定义类似def checkAtk(AttackChosen, health, mana, money):
的函数将创建名为health
,mana
,money
的局部变量,而不是使用您定义的全局变量。这意味着您需要将这些局部变量返回到原始调用,即:
checkAtk(chosenAttack, health, mana, money)
尝试将其替换为:
health, mana, money = checkAtk(chosenAttack, health, mana, money)
并在checkAtk内部执行以下操作并结束:
return health, mana, money
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()