进入我的循环时的Python代码意外行为[关闭]
Posted
技术标签:
【中文标题】进入我的循环时的Python代码意外行为[关闭]【英文标题】:Python code unexpected behavior when entering my loop [closed] 【发布时间】: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 函数,这意味着输出看起来像这样 【参考方案1】:我认为这会起作用...
这是你出错的地方:
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()
【讨论】:
试过还是重启了chooseAtk函数 @user3468656 再试一次-.- 更正它陷入无限循环 @DarthzVayder 好吧,你并没有真正表现出自己解决问题的任何努力,所以遇到被称为 Torxed The Mighty 的狂暴者 -.- 在那里,也修复了你的无限循环(因为你'显然对自己解决任何问题不感兴趣。在为您开发整个游戏时,我们还能为您提供什么帮助?) @DarthzVayder 是的,我删除了大量与代码问题无关的输出、睡眠和其他内容。我们不是来玩游戏或等待您的内容加载的,我们来这里是为了解决编程问题,所以请在发布之前去除您的代码中不相关的信息。因为您可以稍后添加对话。谢谢!【参考方案2】:在这个函数调用中:
checkAtk(chooseAtk, health, mana, money)
chooseAtk
参数无法按预期工作,它传递的是函数而不是返回值。
考虑一下:
>>> def a():
return 'a'
>>> def b(a):
print a
>>> b(a)
<function a at 0x01E5C130>
>>> b(a())
a
【讨论】:
以上是关于进入我的循环时的Python代码意外行为[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
在 C++ 中比较两个 std::string 时的未定义行为 [关闭]
调用 setSortingEnabled(1) 时的意外行为