鱼C论坛

 找回密码
 立即注册
查看: 3707|回复: 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)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-5 20:54:24 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  1. import random

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

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

  12. clue = []
  13. i = 0
  14. while i < len(secret_word):
  15.     clue.append('?')
  16.     i += 1

  17. guessed_word_correctly = False
  18. unknown_letters = len(secret_word)
  19. def update_clue(guessed_letter, secret_word, clue, unknown_letters):
  20.     j = 0
  21.     while j < len(secret_word):
  22.         if guessed_letter == secret_word[j]:
  23.             clue[j] = guessed_letter
  24.             unknown_letters -= 1
  25.         j += 1
  26.    
  27.     return unknown_letters

  28. while lives > 0:
  29.     print(clue)
  30.     print('剩余生命值:' + str(lives))
  31.     guess = input('猜单词中的一个字母或者整个单词:')
  32.     if guess == secret_word:
  33.         guessed_word_correctly = True
  34.         break
  35.    
  36.     if guess in secret_word:
  37.         unknown_letters = update_clue(guess, secret_word, clue, unknown_letters)
  38.     else:
  39.         print('错误!你失去了一条生命。')
  40.         lives -= 1
  41.    
  42.     if unknown_letters == 0:
  43.         guessed_word_correctly = True
  44.         break

  45. if guessed_word_correctly:
  46.     print('你赢了!这个单词是' + secret_word)
  47. else:
  48.     print('你输了!这个单词是' + secret_word)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 22:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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