【问题标题】:Using two counters in a loop在循环中使用两个计数器
【发布时间】:2014-10-04 15:20:55
【问题描述】:

我一直在家里使用 Python 制作基于文本的游戏,但在创建玩家和敌人之间的一小部分战斗时遇到了麻烦。战斗应该包括两个随机掷骰;一个给玩家,一个给外星人。如果外星人掷得比玩家高,则应将 +1 添加到 alien_wins 计数器,如果玩家获胜,则应将 +1 添加到 player_wins 计数器。

我想要发生的是当 player_wins 计数器达到 3 时,循环将停止并产生一条消息,当 alien_wins 达到 3 时也会发生同样的情况,但我无法完成这项工作。任何帮助将不胜感激,谢谢!

import random
from Tkinter import *

def Fight():

    player_wins = 0
    alien_wins = 0

    while player_wins <= 3:

        player_roll = random.randint(0, 10)
        alien_roll = random.randint(0, 7)

        if player_roll > alien_roll:
            contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")
            player_wins += 1
            return

        elif alien_roll > player_roll:
            contents.set("The alien reaches out and strikes you with its claws.")
            alien_wins += 1
            return

        elif alien_roll == player_roll:
            contents.set("You both grapple eachother and eventually back off.")        
            return

            if player_wins == 3:
                contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")
                win = True
                break
            elif alien_wins == 3:
                contents.set("You lose the fight to the alien. Game Over!")
                break

base = Tk()

contents = StringVar()
display = Message(base, textvariable = contents, relief=RAISED)
display.pack()

enterbutton = Button(base, text = 'Try Again', command = Fight).pack()

base.mainloop()

【问题讨论】:

  • 那么,你想在player_wins &lt;= 3alien_wins &lt;= 3继续循环吗?
  • 第一次迭代时return为什么会有循环?另外,为什么return(在alien_roll == player_roll分支中)之后有代码?
  • 是的,没错。我希望它循环,直到两者之一达到 3,然后产生一条消息。
  • 抱歉,我不确定那段代码。

标签: python if-statement while-loop tkinter


【解决方案1】:

当任一条件(点 True 时,您想继续战斗(循环)。您需要在while 循环中添加另一个条件。

您只想在战斗结束后从函数返回,因此删除所有这些返回并在战斗结束后添加一个在循环之外的返回。此外,您只想在战斗结束后创建消息。

def Fight():

    player_wins = 0
    alien_wins = 0

    while player_wins <= 3 and alien_wins <= 3:

        player_roll = random.randint(0, 10)
        alien_roll = random.randint(0, 7)

        if player_roll > alien_roll:
            contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")
            player_wins += 1

        elif alien_roll > player_roll:
            contents.set("The alien reaches out and strikes you with its claws.")
            alien_wins += 1

        elif alien_roll == player_roll:
            contents.set("You both grapple eachother and eventually back off.")        

    if player_wins == 3:
        contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")
        win = True
    elif alien_wins == 3:
        contents.set("You lose the fight to the alien. Game Over!")
    return

【讨论】:

  • 非常感谢您的帮助!
  • 它有助于用文字写下您要完成的工作,然后查看您所写的内容并寻找功能块,并可能沿着这些块重新组织单词,然后将这些块转换为语句或函数或其他东西。 这些词还可以帮助记录您的代码,因此当您在一个月后查看它时,您就会明白您做了什么或试图做什么。
【解决方案2】:

我清理了一些东西。通过使循环依赖于player_winsalien_wins&lt;= 3,您的循环的主要问题得到了解决。这样,只要其中一个命中3,循环就会终止,并且控制转到下面的if/else 语句,该语句将设置获胜者。

def Fight():    
    player_wins = 0
    alien_wins = 0

    while player_wins <= 3 and alien_wins <= 3:    
        player_roll = random.randint(0, 10)
        alien_roll = random.randint(0, 7)

        if player_roll > alien_roll:
            contents.set("You manage to fire a shot of your laser pistol and the alien backs off.")
            player_wins += 1
        elif alien_roll > player_roll:
            contents.set("The alien reaches out and strikes you with its claws.")
            alien_wins += 1
        else:
            contents.set("You both grapple eachother and eventually back off.")     

    if player_wins == 3:
        contents.set("You manage to overcome the alien. It leaps from wall to wall and crawls into the vents.")
        win = True
    else:
        contents.set("You lose the fight to the alien. Game Over!")

您也不需要那些break 语句,因为这完全脱离了循环。您可能正在考虑continue,它只会进入循环的下一次迭代,但即使这样也不需要,因为循环的全部内容只是一个if/else 语句,因此只会执行一个分支。

此外,您检查两个rolls 是否相等的elif 是不必要的,因为您已经排除了两者都不大于/小于另一个,因此可以假设它们相等。最后的if/else 也是如此,您可以在其中检查谁拥有3。如果我们不在循环中,我们知道有人有3,所以我们只需要实际检查单个玩家的得分。

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2019-03-14
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多