鱼C论坛

 找回密码
 立即注册
查看: 2359|回复: 2

[学习笔记] 猜单词游戏(python)

[复制链接]
发表于 2022-12-5 11:15:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random

difficulty = int(input('选择难度(输入1、2或3)\n 1 简单\n 2 正常\n 3 困难\n'))
if difficulty == 1:
    lives = 12
elif difficulty == 2:
    lives = 9
else:
    lives = 6

#可自行修改列表“words”的单词
words = ['impossible', 'hopeless', 'quickly', 'democracy', 'science', 'geography', 'establishmentarian', 'electrocardiogram', 'congratulations', 'revolutionization']
secret_word = random.choice(words)

clue = []
i = 0
while i < len(secret_word):
    clue.append('?')
    i += 1

guessed_word_correctly = False
unknown_letters = len(secret_word)
def update_clue(guessed_letter, secret_word, clue, unknown_letters):
    j = 0
    while j < len(secret_word):
        if guessed_letter == secret_word[j]:
            clue[j] = guessed_letter
            unknown_letters -= 1
        j += 1
   
    return unknown_letters

while lives > 0:
    print(clue)
    print('剩余生命值:' + str(lives))
    guess = input('猜单词中的一个字母或者整个单词:')
    if guess == secret_word:
        guessed_word_correctly = True
        break
   
    if guess in secret_word:
        unknown_letters = update_clue(guess, secret_word, clue, unknown_letters)
    else:
        print('错误!你失去了一条生命。')
        lives -= 1
   
    if unknown_letters == 0:
        guessed_word_correctly = True
        break

if guessed_word_correctly:
    print('你赢了!这个单词是' + secret_word)
else:
    print('你输了!这个单词是' + secret_word)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-5 20:54:24 From FishC Mobile | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-3 16:06:04 | 显示全部楼层
重发
import random

difficulty = int(input('选择难度(输入1、2或3)\n 1 简单\n 2 正常\n 3 困难\n'))
if difficulty == 1:
    lives = 12
elif difficulty == 2:
    lives = 9
else:
    lives = 6

#可自行修改列表“words”的单词
words = ['impossible', 'hopeless', 'quickly', 'democracy', 'science', 'geography', 'establishmentarian', 'electrocardiogram', 'congratulations', 'revolutionization']
secret_word = random.choice(words)

clue = []
i = 0
while i < len(secret_word):
    clue.append('?')
    i += 1

guessed_word_correctly = False
unknown_letters = len(secret_word)
def update_clue(guessed_letter, secret_word, clue, unknown_letters):
    j = 0
    while j < len(secret_word):
        if guessed_letter == secret_word[j]:
            clue[j] = guessed_letter
            unknown_letters -= 1
        j += 1
   
    return unknown_letters

while lives > 0:
    print(clue)
    print('剩余生命值:' + str(lives))
    guess = input('猜单词中的一个字母或者整个单词:')
    if guess == secret_word:
        guessed_word_correctly = True
        break
   
    if guess in secret_word:
        unknown_letters = update_clue(guess, secret_word, clue, unknown_letters)
    else:
        print('错误!你失去了一条生命。')
        lives -= 1
   
    if unknown_letters == 0:
        guessed_word_correctly = True
        break

if guessed_word_correctly:
    print('你赢了!这个单词是' + secret_word)
else:
    print('你输了!这个单词是' + secret_word)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-16 16:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表