【问题标题】:Where should I put an if function for my Guessing game?我应该在哪里为我的 Guessing 游戏添加 if 函数?
【发布时间】:2021-05-24 13:46:46
【问题描述】:

现在,我正在为绝对初学者编写 Python 一书的第 3 章。

第 3 章末尾的挑战之一是“修改猜我的号码游戏,使玩家的猜测次数有限”,如果玩家未能获得正确的猜测次数,则应发送一条消息显示出来。

代码如下所示:

# Guess My Number
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher...")
            
    guess = int(input("Take a guess: "))
    tries += 1

print("You guessed it!  The number was", the_number)
print("And it only took you", tries, "tries!\n")
  
input("\n\nPress the enter key to exit.")

据我目前所见,我需要添加一个变量来计算玩家拥有多少生命,该变量在开始时设置为一个数量,例如 10,并且应该使用 if 命令来确保,当玩家使用所有生命时,会显示该消息。

但是,我不确定将 if 命令放在现有代码中的什么位置。

【问题讨论】:

    标签: python if-statement variables


    【解决方案1】:

    好吧,如果我是你,我会制作一个游戏循环而不是猜测循环。然后当我达到猜测极限时,我会打破游戏循环。但是,如果你想保留你的代码,你可以使用它。

    while guess != the_number:
        if tries == 3: # Replace 3 with the limit you'd like to use
            print("You lost :(")
            exit()
        else:
            if guess > the_number:
                print("Lower...")
            else:
                print("Higher...")
            
        guess = int(input("Take a guess: "))
        tries += 1
    

    在你的情况下也不要使用break,它仍然会导致最终打印获胜的消息

    (间距可能有点偏,所以你可能需要修复它)

    【讨论】:

    • 就个人而言,我觉得有点奇怪的是,当没有超过尝试次数时,一些代码在 else 中,而另一些不在 - 我会将代码移出else 块,这样你剩下的是一个保护条款。我认为这会更清楚,尽管这显然是主观的。
    • 我根本不是在谈论性能(我认为不会有任何区别),我只是在谈论代码的易读性。
    【解决方案2】:

    在你说tries+=1 之后,添加一个 if 语句。您的代码应如下所示:

    if tries>3:
        print("Game Over")
        break()
    

    【讨论】:

    • 如果我的回答对您有帮助,请标记绿色复选标记并点赞,以便对社区有所帮助。
    • 抱歉,我删除了关于 break 的评论(顺便说一句,这是一个声明,而不是一个函数),因为我意识到这还不够——首先,你甚至可以得到一个游戏结束消息如果你最后的猜测是正确的,其次你会得到一个“你猜对了!”即使你没有发消息
    【解决方案3】:
    import random  
    
    print("\tWelcome to 'Guess My Number'!")
    print("\nI'm thinking of a number between 1 and 100.")
    print("Try to guess it in as few attempts as possible.\n")
    
    # set the initial values
    the_number = random.randint(1, 100)
    guess = int(input("Take a guess: "))
    tries = 1
    
    while tries < 8:
        if guess == the_number:
            print("You guessed it!  The number was", the_number)
            print("And it only took you", tries, "tries!\n")
            break
        elif guess > the_number:
            print("Lower...")
        else:
            print("Higher...")
                
        guess = int(input("Take a guess: "))
        tries += 1
    
    if tries == 8:
        print("You failed to guess my number")
    
    input("\n\nPress the enter key to exit.")
    

    你也可以这样做

    【讨论】:

      猜你喜欢
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多