Bagels系列|python小程序随手记

来自《The Big Book of Small Python Projects》

python 复制代码
import random

NUM_DIGITS = 3  
MAX_GUESSES = 10  

def main():
    print('''Bagels, a deductive logic game.
I am thinking of a {}-digit number with no repeated digits.
Try to guess what it is. Here are some clues:
When I say:    That means:
  Pico         One digit is correct but in the wrong position.
  Fermi        One digit is correct and in the right position.
  Bagels       No digit is correct.

For example, if the secret number was 248 and your guess was 843, the
clues would be Fermi Pico.'''.format(NUM_DIGITS))

    while True:  # Main 主游戏循环.
        # 存储玩家需要猜测的秘密数字
        secretNum = getSecretNum()
        print('I have thought up a number.')
        print(' You have {} guesses to get it.'.format(MAX_GUESSES))

        numGuesses = 1
        while numGuesses <= MAX_GUESSES:
            guess = ''
            # 继续循环,直到用户输入一个有效的猜测
            while len(guess) != NUM_DIGITS or not guess.isdecimal():
                print('Guess #{}: '.format(numGuesses))
                guess = input('> ')

            clues = getClues(guess, secretNum)
            print(clues)
            numGuesses += 1

            if guess == secretNum:
                break  # 猜对跳出循环
            if numGuesses > MAX_GUESSES:
                print('You ran out of guesses.') 
                print('The answer was {}.'.format(secretNum))

        # 询问玩家是否想再玩一次。
        print('Do you want to play again? (yes or no)')
        if not input('> ').lower().startswith('y'):
            break
    print('Thanks for playing!')


def getSecretNum():
    """返回一个由NUM_DIGITS唯一随机数字组成的字符串。"""
    numbers = list('0123456789')  
    random.shuffle(numbers)  # 将它们随机排列

    # 获取秘密号码列表中的第一个NUM_DIGITS数字
    secretNum = ''
    for i in range(NUM_DIGITS):
        secretNum += str(numbers[i])
    return secretNum


def getClues(guess, secretNum):
    """返回带有pico, fermi, bagels线索的字符串。"""
    if guess == secretNum:
        return 'You got it!'

    clues = []

    for i in range(len(guess)):
        if guess[i] == secretNum[i]:
            # 正确的数字在正确的位置,提示Fermi。
            clues.append('Fermi')
        elif guess[i] in secretNum:
            # 正确的数字在错误的位置,提示Pico。
            clues.append('Pico')
    if len(clues) == 0:
        return 'Bagels'  # 没有正确的数字
    else:
        # 将线索按字母顺序排列,这样它们原来的顺序就不会泄露信息。
        clues.sort()
        # 从字符串线索列表中制作一个字符串。
        return ' '.join(clues)

if __name__ == '__main__':
    main()

运行效果:

Bagels系列|python小程序随手记

相关推荐
易辰君13 分钟前
python爬虫 - 深入requests模块
开发语言·爬虫·python
人工智障调包侠13 分钟前
基于深度学习多层感知机进行手机价格预测
人工智能·python·深度学习·机器学习·数据分析
hmz85615 分钟前
最新网课搜题答案查询小程序源码/题库多接口微信小程序源码+自带流量主
前端·微信小程序·小程序
计算机编程-吉哥1 小时前
计算机毕业设计 基于Python的智能文献管理系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·毕业设计·计算机毕业论文·计算机毕业设计选题·软件工程毕业设计论文·文献管理系统
笃励1 小时前
Java面试题二
java·开发语言·python
一颗星星辰2 小时前
Python | 第九章 | 排序和查找
服务器·网络·python
打码人的日常分享2 小时前
企业人力资源管理,人事档案管理,绩效考核,五险一金,招聘培训,薪酬管理一体化管理系统(源码)
java·数据库·python·需求分析·规格说明书
27669582922 小时前
京东e卡滑块 分析
java·javascript·python·node.js·go·滑块·京东
unix2linux2 小时前
Parade Series - SHA256
linux·python·mysql·shell
巽星石3 小时前
【Blender Python】7.一些运算、三角函数以及随机
python·blender·三角函数·随机·环形阵列