【问题标题】:Python Guess game with tries option带有尝试选项的 Python 猜谜游戏
【发布时间】:2020-05-22 17:40:42
【问题描述】:

我正在开发一个 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")

【问题讨论】:

  • 欢迎朱利安!你能具体说一下你要解决的问题是什么吗?
  • @Jon 如果有人连续猜 X 次,他只想数一次
  • 您可以将set() 用于先前猜测的数字,并检查数字是否在先前猜测的集合中
  • 请注意,您使用的是 10 和 100,因为它们是相同的值。您描述的代码似乎不像您解释的那样工作。你的问题是什么?
  • 另外,您可能希望在用户猜对的条件正文中添加一个中断

标签: python python-3.x helper


【解决方案1】:

首先要做的事情。您声称该数字在 1 到 100 之间,并且只选择了 1 到 10 之间的数字。

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

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

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

使用集合,您可以轻松检查数字是否已经被猜到,如下所示:

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

最后,您可以在阅读猜测后立即添加 1,而不是在每个 if 中尝试添加 1。您还可以将 elif userInput &gt; 100elif userInput &lt; 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 异常。

【讨论】:

  • 嗨,迈克,感谢您的编辑建议和回答。
  • 不客气。如果您觉得它有用,您可以接受它(通过单击投票计数器下方的“勾选”按钮)。如果没有,请告诉我如何提供帮助。
【解决方案2】:

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

此外,如果用户给出的数字超出了允许的范围,我猜你不应该增加尝试次数。此外,您应该检查输入是否大于 100 而不是 10,您可以在同一个 elif 语句中执行此操作。例如:

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")

【讨论】:

  • 再次嗨,非常感谢!
【解决方案3】:

使用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")

【讨论】:

  • 代码永远不会达到你写的条件。也许你应该写在if之后。
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多