Python 为什么这个程序会重复?
我正试着从练习36中得到一个游戏来工作 我可以让游戏的整个部分运作,除非是在战斗中。我认为问题就在这里Python 为什么这个程序会重复?,python,Python,我正试着从练习36中得到一个游戏来工作 我可以让游戏的整个部分运作,除非是在战斗中。我认为问题就在这里 def combat_engine(): global wins, hit_points, current_hp if monster_current_hp or current_hp > 0: print "You can type 'a' for attack, or 'q' for quit, choose one." ans
def combat_engine():
global wins, hit_points, current_hp
if monster_current_hp or current_hp > 0:
print "You can type 'a' for attack, or 'q' for quit, choose one."
answer = raw_input(":> ")
if 'a' in answer:
attack(monster)
attack('player')
combat_engine()
elif 'q' in answer:
print t.white_on_red("You give up and are devoured by the %s.") % monster
exit(0)
else:
print "I dont understand %s." % answer
next()
combat_engine()
elif monster_hp == 0:
print "You defeated the %s, congradulations %s!" % monster, name
wins = wins + 1
if wins == 5:
you_win()
elif victim == 'player':
hit_points = hit_points + d6()
current_hp = hit_points
print "You feel hardier."
next()
barracks()
elif current_hp == 0:
print "You have been deafeted by the %s, better luck next time." % monster
next()
exit(0)
else:
print "Bug Somewhere"
我相信错误就在这个函数的某个地方。当我打印出每个角色的生命值时,怪物的生命值降低到-2后,战斗仍在继续。也许这是布尔人的问题
我想让它做的是,要么做所有的统计调整,以赢得和退出游戏,如果你输了。我只想克服这些,这样我就可以开始学习课程和听写了,这会让我的生活更轻松。请让我知道,如果我应该发布更多的信息,我在这方面是新的,并不是最好的张贴在这里的问题还没有
提前谢谢 在代码的最后一部分,您没有调用函数d100(),而是调用值d100。那肯定不是你想做的
if d20() >= monster_to_hit and d100() <= monster_crit:
print "The %s scored a critical hit against you!" % monster
hit_points = hit_points - (monster_dmg * 3)
next()
elif d20 >= monster_to_hit and d100() > crit:
print "The %s strikes you!"
hit_points = hit_points - monster_dmg
next()
如果d20()>=怪物对命中和d100()=怪物对命中和d100()>暴击:
打印“你被%s击中了!”
命中率=命中率-怪物dmg
下一个()
在python中调试时,主单词是“print”。你应该尽可能自由地打印,以便了解正在发生的事情
例如:
# attack function
def attack(victim):
dice20 = d20()
dice100 = d100()
print "victim == monster", victim == monster
print dice20
print dice100
print to_hit
print crit
print monster_current_hp
print mod_dmg
print mod_dmg * 3
print monster_to_hit
print monster_crit
print hit_points
print monster_dmg
print monster_dmg * 3
global monster_current_hp, current_hp, to_hit, crit, hit_points
if victim == monster:
if dice20 >= to_hit and dice100 <= crit:
print "You scored a critical hit against the %s!" % monster
monster_current_hp = monster_current_hp - (mod_dmg * 3)
next()
elif dice20 >= to_hit and dice100 > crit:
print "You strike the %s!" % monster
monster_current_hp = monster_current_hp - mod_dmg
next()
else:
print "The %s evades your attack!" % monster
next()
else:
if dice20 >= monster_to_hit and dice100 <= monster_crit:
print "The %s scored a critical hit against you!" % monster
hit_points = hit_points - (monster_dmg * 3)
next()
elif dice20 >= monster_to_hit and dice100 > crit:
print "The %s strikes you!"
hit_points = hit_points - monster_dmg
next()
#攻击功能
def攻击(受害者):
dice20=d20()
dice100=d100()
打印“受害者==怪物”,受害者==怪物
印刷骰子20
打印骰子100
打印到
打印暴击
打印怪物\u当前\u hp
打印模块dmg
打印模块dmg*3
将怪物打印到命中
打印怪物暴击
打印生命值
打印monster_dmg
打印monster_dmg*3
全球怪物当前生命,当前生命,目标命中,暴击,命中点
如果受害者==怪物:
如果骰子20>=命中骰子100=命中骰子100>暴击:
打印“你击中了%s!”%monster
怪物当前生命=怪物当前生命-模式dmg
下一个()
其他:
打印“%s躲过你的攻击!”%monster
下一个()
其他:
如果骰子20>=怪物掷骰命中骰子100=怪物掷骰子命中骰子100>暴击:
打印“你被%s击中了!”
命中率=命中率-怪物dmg
下一个()
在战斗引擎中执行同样的操作,以便看到无限循环出现。然后删除那些用无用信息填充跟踪的无用打印,直到您清楚地看到它循环的原因,因为某些值,因为一个布尔if测试的行为不符合您的预期。。。诸如此类。
如果monster_current_hp或current_hp>0有两种缺陷:
首先,它只是测试monster_current_hp的计算结果是否为真,而不是针对0进行测试;第二,即使这是固定的,该声明将继续程序,如果任何一架战斗机有积极的HP,而假设你想停止时,任何一架战斗机有消极的HP。试试这个:如果怪物\u当前\u hp>0和当前\u hp>0:
一旦你这样做了,在大多数情况下,你会开始进入“bug某处”这一行。这是因为
elif monster\u hp==0:
和elif current\u hp==0:
在hp从正变负时不会触发。使用尝试减少代码样本-删除不重要的代码行,直到只有触发错误的代码行。这涉及到摆脱实战-看看你是否可以硬编码一系列导致意外行为的事件。原因是你的代码样本很难通过阅读来查看,这个过程可能会帮助你找到错误。老实说,我不太确定哪些行是不必要的,虽然我认为问题出在关于战斗引擎功能的第一个“如果”声明中。也许我误解了你?作为一个新手很糟糕。在我的问题中添加这些函数的调用位置和/或我得到的输出是否有意义?关键是最终或多或少地知道问题的原因在哪里——你现在知道该关注什么了。这将引导你自己解决问题,或者让阅读你的问题的人更容易完成这项任务。这大大增加了快速获得好答案的可能性。另一个附带的好处是,你的问题将不太具体到你的情况,因此更有可能帮助未来的访客。谢谢你注意到这一点。似乎没有解决我的问题,但我完全没有解决。在Python中消除bug的关键是打印值。在每个比较中,添加print以了解背后的逻辑。我将编辑并向您展示我必须添加的“调试”打印以检查跟踪。这有助于更好地理解正在发生的事情,我将在编辑中展开