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小程序随手记

相关推荐
nbsaas-boot4 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯4 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_7894 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
2501_915918417 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
说私域8 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
難釋懷8 小时前
第一个小程序
小程序
春哥的研究所8 小时前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
weixin_lynhgworld8 小时前
盲盒一番赏小程序技术实现方案:高并发与防作弊的平衡之道
小程序
chao_7899 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴9 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python