如何在 Python 中为随机数猜谜游戏生成新数字?

how to generate a new number for a random number guessing game in python?

提问人:Pearchy 提问时间:8/29/2023 更新时间:8/29/2023 访问量:142

问:

我希望标题是不言自明的,但要了解更多上下文: 我想编写一个随机数猜谜游戏,而不是在用户猜对时结束游戏,我问玩家是否想再玩一次,在他们回答“是”的情况下,代码会生成一个新数字,游戏重复。

这是代码:

# import time and random values
import random
import time


# tracks the number of attempts
attempts = 0

#introduction
print("welcome to my number guessing game! ")
input("press any key to start playing ")

while True:
# ask user for input
    number1 = random.randint(0,10)
    guess = int(input("guess a random number between 0 and 10. "))
    attempts += 1

# verify if guess is correct
    if guess == number1:
        playAgain = input(f"correct! the secret number was indeed {number1}, and you guessed it in {attempts} attempts. play again? (yes/no) ")
        if playAgain == "yes":
            # generate a new number and make the user try again
            number1 = random.randint(0,10) 
            continue
        else:
            break
    elif guess > number1:
        print("try a smaller number. ")
    else:
        print("try a bigger number. ")
    

它不是只在用户猜对并决定再次玩时生成一个新号码,而是在用户玩游戏时生成一个新号码,因此游戏永远不会真正结束。如何解决此问题?

python-3.x while-loop

评论

1赞 mozway 8/29/2023
将第一个移到number1 = random.randint(0,10)while
0赞 not_speshal 8/29/2023
@mozway - 仅仅这样做并不能使游戏按需运行。具体来说,“代码生成一个新数字,游戏重复。
0赞 mozway 8/29/2023
@not_speshal公平地说,我还没有检查整个事情。但这显然是纠正它的第一步;)

答:

2赞 Gxogac 8/29/2023 #1

更改您的

while True:
# ask user for input
    number1 = random.randint(0,10)
    guess = int(input("guess a random number between 0 and 10. "))
    attempts += 1

while True:
    # Generate a new number for each game
    number1 = random.randint(0, 10)

    while True:
    # ask user for input
        guess = int(input("Guess a random number between 0 and 10: "))
        attempts += 1
0赞 bbart23 8/29/2023 #2

你的 在你的number1 = random.randint(0,10)while-loop

将您的第一个放在 while 循环上方,并在他们再次播放时将您的尝试设置回 0。number1 = random.randint(0,10)

你拥有它的方式将使它成为这样,每次用户猜测时,它都将是一个新生成的数字(不是预期的)。

以下是固定代码:

import random
import time

# tracks the number of attempts
attempts = 0

# introduction
print("Welcome to my number guessing game!")
input("Press any key to start playing ")

# Generate the random number outside the loop
number1 = random.randint(0, 10)

while True:
    # ask user for input
    guess = int(input("Guess a random number between 0 and 10. "))
    attempts += 1

    # verify if guess is correct
    if guess == number1:
        playAgain = input(f"Correct! The secret number was indeed {number1}, and you guessed it in {attempts} attempts. Play again? (yes/no) ")
        if playAgain == "yes":
            # reset attempts and generate a new number
            attempts = 0
            number1 = random.randint(0, 10)
        else:
            break
    elif guess > number1:
        print("Try a smaller number.")
    else:
        print("Try a bigger number.")
1赞 not_speshal 8/29/2023 #3

使用双循环的现有代码的另一种变体:while

while True:
    #restart the game and generate number to guess
    number1 = random.randint(0,10)
    attempts  = 0
    
    guess = int(input("guess a random number between 0 and 10. "))
    attempts += 1
    
    #repeat this logic while the guess is not correct
    while guess!=number1:
        if guess > number1:
            print("try a smaller number. ")
        else:
            print("try a bigger number. ")
        guess = int(input("guess a random number between 0 and 10. "))
    
    #ask to continue once the previous game is completed
    playAgain = input(f"correct! the secret number was indeed {number1}, and you guessed it in {attempts} attempts. play again? (yes/no)")
    if playAgain == "no":
        break