Python 带有尝试选项的猜谜游戏

Python Guess game with tries option

我正在研究 python 猜谜游戏,以了解 python 的工作原理。

我想添加一个选项来计算猜测的次数,但如果玩家多次给出相同的答案,我想将其计为 1 次尝试。

我不知道如何进行。任何帮助将不胜感激:)

这是我当前的脚本

# The Guess Game   

# secret number between 1 and 10 
import random
randomNumber = random.randrange(1,10)
#print randomNumber #check if it's working
tries = 0

# rules
print('Hello and welcome to the guess game !')
print('The number is between 1 and 10')

guessed = False
tries += 1

while guessed==False:

    userInput = int(input("Please enter your guess: "))

    if userInput==randomNumber:
        guessed = True
        tries = str(tries)
        print("Congratulations ! You win after " + tries + " tries ! ")

    elif userInput>10:
        print("The guess range is between 1 and 10, please try again")
        tries = tries + 1

    elif userInput<1:
        print("The guess range is between 1 and 10, please try again")
        tries = tries + 1

    elif userInput>randomNumber:
        print("Your guess is too large")
        tries = tries + 1

    elif userInput < randomNumber:
        print("Your guess is too small")
        tries = tries + 1

print("End of the game, please play again")

不确定您要使用代码完成什么。然而,如果您希望用户重复相同的次数不计为另一次尝试,您应该记录已经估算的尝试(例如在数组中)并检查当前尝试是否在事先已经使用过的尝试中。

此外,如果用户给出的数字超出允许范围,我想你不应该增加尝试次数。您还应该检查输入是否大于 100 而不是 10,并且您可以在同一个 elif statement.Like:

中进行
elif userInput<1 or userInput>100:

按照你的要求,我给你一个完整的例子:

# The Guess Game   

# secret number between 1 and 100 
import random
randomNumber = random.randrange(1,10)
#print randomNumber #check if it's working
tries = 0

# rules
print('Hello and welcome to the guess game !')
print('The number is between 1 and 10')

guessed = False
tries += 1
past_tries = set()

while guessed==False:

    userInput = int(input("Please enter your guess: "))

    if userInput in past_tries:
        print("This guess is repeated!")

    elif userInput==randomNumber:
        guessed = True
        tries = str(tries)
        print("Congratulations ! You win after " + tries + " tries ! ")

    elif userInput>10 or userInput<1:
        print("The guess range is between 1 and 100, please try again")

    elif userInput>randomNumber:
        print("Your guess is too large")
        tries = tries + 1

    elif userInput < randomNumber:
        print("Your guess is too small")
        tries = tries + 1

    past_tries.add(userInput)

print("End of the game, please play again")

你可以用一个set来存储用户的猜测,集合数据结构不允许重复。您可以在用户猜对后刷新集合。

使用 set 来跟踪用户已经给出的输入。如果用户输入已存在于集合中,请不要增加 tries 计数器。

# The Guess Game

# secret number between 1 and 100
import random
randomNumber = random.randrange(1,10)
#print randomNumber #check if it's working
tries = 0

# rules
print('Hello and welcome to the guess game !')
print('The number is between 1 and 100')

guessed = False
tries += 1
user_inputs = set()  # to keep track of inputs

while not guessed:

    userInput = int(input("Please enter your guess: "))
    user_inputs.add(userInput)

    if userInput==randomNumber:
        guessed = True
        tries = str(tries)
        print("Congratulations ! You win after " + tries + " tries ! ")

    elif userInput in user_inputs:  # if already seen this guess, don't increment the counter
        print("You had already guessed this number earlier")
        continue

    elif userInput>10:
        print("The guess range is between 1 and 100, please try again")
        tries = tries + 1

    elif userInput<1:
        print("The guess range is between 1 and 100, please try again")
        tries = tries + 1

    elif userInput>randomNumber:
        print("Your guess is too large")
        tries = tries + 1

    elif userInput < randomNumber:
        print("Your guess is too small")
        tries = tries + 1

print("End of the game, please play again")

要事第一。您声称的数字介于 1 和 100 之间,并且只选择了介于 1 和 10 之间的数字。

randomNumber = random.randrange(1,10)更改为randomNumber = random.randrange(1, 100)

您还在验证用户输入时检查数字是否大于 10 而不是 100。将 elif userInput>10 更改为 elif userInput > 100

现在,正如其他人所提到的,为了跟踪猜测,您可以使用 set。集合是一种数据结构(基本上是一种存储信息的方式),它只允许您添加到其中的每个不同项目的单个副本。

使用集合,您可以像这样轻松检查数字是否已经被猜到:

guess = int(input())
if guess not in guesses:
    guesses.add(guess)

最后,您可以在阅读猜测后立即加 1,而不是在每个 if 中尝试加 1。您还可以将 elif userInput > 100elif userInput < 1 合并在一起,因为它们打印相同的内容。

完整代码:

# The Guess Game   

# secret number between 1 and 100 
import random
randomNumber = random.randrange(1, 100)  # changed from 10 to 100
#print randomNumber #check if it's working

# rules
print('Hello and welcome to the guess game !')
print('The number is between 1 and 100')

guesses = set()  # your set of guesses
guessed = False
tries = 0        # no need add 1 to the tries here

while guessed == False:

    userInput = int(input("Please enter your guess: "))

    if userInput not in guesses:   # if it's not in our set
        tries += 1                 # increase the tries
        guesses.add(userInput)     # add it to our set

    if userInput == randomNumber:
        guessed = True
        tries = str(tries)
        print("Congratulations ! You win after " + tries + " tries ! ")

    elif userInput > 100 or userInput < 1:
        print("The guess range is between 1 and 100, please try again")

    elif userInput > randomNumber:
        print("Your guess is too large")

    elif userInput < randomNumber:
        print("Your guess is too small")

print("End of the game, please play again")

额外:总是确保您的用户给您提供您期望的输入。查看当您 运行 这个程序和用户输入 "asdfg" 时会发生什么。提示:查看 python 异常。