【发布时间】: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