yj311 发表于 2022-12-5 11:15:25

猜单词游戏(python)

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:
            clue = 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)

PythonQishi 发表于 2022-12-5 20:54:24

yj311 发表于 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:
            clue = 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)
页: [1]
查看完整版本: 猜单词游戏(python)