python超级有用的实战项目,拿走不谢~

写在前面的一点P话:

Python是目前最好的编程语言之一。由于其可读性和对初学者的友好性,已被广泛使用。

那么要想学会并掌握Python,可以实战的练习项目是必不可少的。

直接上第一个项目~

猜字游戏

在这个游戏中,你必须一个字母一个字母的猜出秘密单词。

如果你猜错了一个字母,你将丢掉一条命。

正如游戏名那样,你需要仔细选择字母,因为你的生命数量非常有限。

###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
import random

# 生命次数
lives = 3

# 神秘单词, 随机选择
words = ['pizza', 'fairy', 'teeth', 'shirt', 'otter', 'plane']
secret_word = random.choice(words)
# print(secret_word)

clue = list('?????')
heart_symbol = u'\u2764'

guessed_word_correctly = False


def update_clue(guessed_letter, secret_word, clue):
    index = 0
    while index < len(secret_word):
        if guessed_letter == secret_word[index]:
            clue[index] = guessed_letter
        index = index + 1


while lives > 0:
    print(clue)
    print('剩余生命次数: ' + heart_symbol * lives)
    guess = input('猜测字母或者是整个单词: ')

    if guess == secret_word:
        guessed_word_correctly = True
        break

    if guess in secret_word:
        update_clue(guess, secret_word, clue)
    else:
        print('错误。你丢了一条命\n')
        lives = lives - 1


if guessed_word_correctly:
    print('你赢了! 秘密单词是 ' + secret_word)
else:
    print('你输了! 秘密单词是 ' + secret_word)

游戏过程

请添加图片描述

猜你喜欢

转载自blog.csdn.net/m0_67575344/article/details/125149482