1 Star 0 Fork 0

ChrisEighteen18/pySimpleGamePractice

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bagels.py 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
# 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ChrisEighteen18/py-simple-game-practice.git
git@gitee.com:ChrisEighteen18/py-simple-game-practice.git
ChrisEighteen18
py-simple-game-practice
pySimpleGamePractice
master

搜索帮助