代码拉取完成,页面将自动刷新
# bagels 游戏
import random
"""
随机生成一个数字(以字符串的形式出现)
"""
def getSecretNum(num):
numDigits = list(range(10))
random.shuffle(numDigits)
secretNum = ""
for i in range(num):
secretNum += str(numDigits[i])
return secretNum
def showGameRule():
print("""
I am thinking of a 3-digit number. Try to guess what it is.
The clues I give are...
When I say: That means:
Bagels None of the digits is correct.
Pico One digit is correct but in the wrong position.
Fermi One digit is correct and in the right position.
I have thought up a number. You have 10 guesses to get it.
""")
def checkGuess(guess, secret):
if guess == secret:
return "Great~ got it~"
check = []
for i in range(len(guess)):
if guess[i] == secret[i]:
check.append("Fermi")
elif guess[i] in secret:
check.append("Pico")
if len(check) == 0:
check.append("bagels")
check.sort()
return " ".join(check)
def isNumber(num):
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
def isPlayAgain():
print("Do u want to play again?")
answer = input().lower()
return answer.startswith('y')
_max_times = 10
_num_long = 3
if __name__ == '__main__':
while True:
showGameRule()
times = 0
print('I have thought up a number. Its long is %s You have %s guesses to get it.' %
(_num_long, _max_times))
secret = getSecretNum(_num_long)
while times <= _max_times:
times = times + 1
guess = ' '
while not isNumber(guess) or len(guess) != _num_long:
print("Enter your guess:")
guess = input()
print(checkGuess(guess, secret))
# 释放while资源
if guess == secret:
print("right~")
break
if times > _max_times:
print('You ran out of guesses. The answer was %s.' % (secret))
if not isPlayAgain():
break
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。