NUM_DIGITS = 3
MAX_GUESS = 10
def getSecrectNum():
'''
随机获取三个数字
:return:
'''
numbers = list(range(10))
random.shuffle(numbers)
secrectNum = ''
for i in range(NUM_DIGITS):
secrectNum += str(numbers[i])
return secrectNum
def getClues(guess, secretNum):
'''
用户输入的数字和,系统产生的秘密数字进行比对
:param guess:
:param secretNum:
:return:
'''
if guess == secretNum:
print('Oh my god , You go it!')
clues = []
for i in range(len(guess)):
if guess[i] == secretNum[i]:
clues.append('Fermi')
elif guess[i] in secretNum:
clues.append('Pico')
if len(clues) == 0:
return 'Bagels'
clues.sort()
return ' '.join(clues)
def isOnlyDigits(num):
'''
判断是否位数字
:param num:
:return:
'''
if num == '':
return False
for i in num:
if i not in '0 1 2 3 4 5 6 7 8 9'.split():
return False
return True
print('I am thinking of a {0}-digit-number. Try to guess what it is .'.format(NUM_DIGITS))
print('The clues i give are...')
time.sleep(1)
print('When i say: That means:')
time.sleep(1)
print('Bagels None of the digits is correct.')
time.sleep(1)
print('Pico None digits is correct but in the wront position.')
time.sleep(1)
print('Fermi None digits is correct and in the right position.')
while True:
secretNum = getSecrectNum()
print('I have thought up a number. you have {0} guesses to get it.'.format(MAX_GUESS))
guessesTaken = 1
while guessesTaken < MAX_GUESS:
guess = ''
while len(guess) != NUM_DIGITS or not isOnlyDigits(guess):
print('Guess #{0}'.format(guessesTaken))
guess = input()
if len(guess) !=NUM_DIGITS:
print('please input three number!')
print(getClues(guess, secretNum))
guessesTaken += 1
if guess == secretNum:
break
if guessesTaken > MAX_GUESS:
print('You ran out or guesses .The answer was {0}.'.format(secretNum))
print('Do yoy want to play again ? ( yes or no)')
if not input().lower().startswith('y'):
break
python 猜单词游戏 代码
最新推荐文章于 2024-10-11 11:47:19 发布
本文介绍如何使用Python编程实现一个猜单词游戏。玩家将根据提示猜测隐藏的单词,通过输入字母来逐步揭示单词。游戏包括错误尝试次数限制,正确猜测后的胜利提示等功能。通过这个项目,读者可以学习到Python的基本语法、条件判断和循环结构。

1863

被折叠的 条评论
为什么被折叠?



