python 猜单词游戏 代码

本文介绍如何使用Python编程实现一个猜单词游戏。玩家将根据提示猜测隐藏的单词,通过输入字母来逐步揭示单词。游戏包括错误尝试次数限制,正确猜测后的胜利提示等功能。通过这个项目,读者可以学习到Python的基本语法、条件判断和循环结构。
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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值