以下是一段简单的猜字谜游戏代码:

import random

riddles = {'1. What has a head and a tail but no body?': 'coin',
           '2. What has a heart that doesn’t beat?': 'artichoke',
           '3. What has a neck but no head?': 'bottle',
           '4. What is always in front of you but can’t be seen?': 'future',
           '5. What starts with an E, ends with an E, but only contains one letter?': 'envelope',
           '6. What has a thumb and four fingers but is not alive?': 'glove',
           '7. What is so fragile that saying its name breaks it?': 'silence',
           '8. What is full of holes but still holds water?': 'sponge',
           '9. What has a bottom at the top?': 'leg',
           '10. What goes up but never comes down?': 'age'}

while True:
    # select a random riddle
    riddle = random.choice(list(riddles.keys()))
    answer = riddles[riddle]
    attempts = 3

    # game loop
    while attempts > 0:
        # print the riddle and get the user's answer
        print(riddle)
        guess = input('Answer: ')
        guess = guess.lower()

        # check if the guess is correct
        if guess == '0':
            print('Goodbye!')
            exit()
        elif guess == answer:
            print('Correct!')
            break
        else:
            attempts -= 1
            print('Wrong!')
            print('You have', attempts, 'attempts left.')
            if attempts == 0:
                print('Game over!')
                print('The answer is', answer)
        
    # ask the user if they want to play again
    play_again = input('Do you want to play again? Y/N: ')
    play_again = play_again.lower()
    if play_again == 'n':
        print('Thanks for playing!')
        break

这段代码使用 Python3 编写,包含 10 道有趣的字谜,每道谜题都提供了三次机会让你猜答案。游戏玩法简单,只需输入你的答案,就能体验解谜的乐趣!如果想退出游戏,输入 0 即可。每完成一轮后,可以选择继续游戏 (Y/y) 或者退出游戏 (N/n)。快来挑战你的脑力,看看你能猜出多少谜题吧!

Python3 猜字谜游戏代码:10 道谜题,三次机会,挑战你的脑力!

原文地址: https://www.cveoy.top/t/topic/oXlw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录