如何让用户向猜字游戏中添加单词

0 投票
2 回答
1832 浏览
提问于 2025-04-27 23:57

我正在用Python 3.x制作一个猜单词游戏(也叫“吊死鬼”)。我已经完成了游戏的基本功能,可以从一个单词数组中随机选择一个单词,让用户来猜,并且有一个生命值系统。不过,我不知道该怎么做才能让用户自己添加单词到游戏里。我查了其他类似的问题,但没有找到解决办法。

这是我的代码:

#Hangman Game

#Asks the user to input their name
name = input("What is your name?")

#This prints the first statement, welcoming the user to the hangman game
print("Hello", name, ".Welcome to BlackHamed's Game of Hangman!")
print("")

#Imports the random module
import random

#These are the words in an array
words = ("python", "computer", "processor")
#This line of code, makes the program choose a random word to use for the game
word = random.choice(words)
#This is the print statement which indicates to the user how long the word is
print("The word you need to guess is", len(word), "letters long. Let's Get Started!")


#This is the 'Lives' system in the game.
lives = 6

if lives == 6:
print("You have 6 lives left.")
if lives == 5:
print("You have 5 lives left.")
if lives == 4:
print("Your have 4 lives left.")
if lives == 3:
print("You have 3 lives left.")
if lives == 2:
print("Your have 2 lives left.")
if lives == 1:
print("You have 1 life left.")

correct_guesses = 0
#This is the empty list where the guessed letters are inputted
Guessed_Letters=[]

while lives > 0:
letter = input ("Guess a letter:")
if letter not in Guessed_Letters:
    Guessed_Letters.append(letter)

    #If the letter is in the word, the program adds 1 point to correct_guesses and outputs a message
if letter in word:
    correct_guesses += 1
    print("Correct. This letter is in the word")
    print(letter)
    print("Letters Already Guessed:", Guessed_Letters)
    print(letter in word)
    #If the letter guessed is not in the word, the program minuses 1 from the lives and outputs a message    
else:
    lives -= 1
    print("Incorrect. This letter is not in the word. You have lost a life. You have", lives, "lives left.")
    print("_")
    print("Letters Already Guessed:", Guessed_Letters)
    #If the number of correct guesses is equal to the length of the word:
    #An output message congratulating the user is displayed, their score is displayed and the 'while' loop is broken      
if correct_guesses == len(word):
    print("Well done, you have got the correct word, which was", word)
    print("SCORE:", correct_guesses)
    break
    #If the number of lives is equal to 0:
    #An output message saying the user has lost and their score is displayed
else:
    if lives == 0:
        print("You have lost the game. The word is", word)
        print("SCORE:", correct_guesses)
#If an letter is inputted twice, the program only displays this message, and doesn't add/take any correct guesses/lives       
else:
    print("You have already guessed that letter, guess a letter different to this one.")

有没有人能帮我写出这个新功能的代码,以及它是怎么工作的?

暂无标签

2 个回答

0

这要看你想让它怎么运作。你可以在游戏开始时问用户是否想玩,或者是否想添加新单词,然后你只需要像这样把新单词加到列表里。

word=input()
words.append(word)

接着你也可以问用户是否想继续玩或者再玩一局。需要注意的是,这些单词只在这一局游戏中有效。如果你想要更永久的效果,就需要学习一下文件输入输出的相关内容。

1

我会这样做:让程序从一个文件中加载单词列表。你可以在和脚本同一个文件夹里放一个叫“words.txt”的文件,用户可以用任何文本编辑器来修改这个文件。

另外,你还可以设置一个可选的命令行参数,让程序使用不同的文件。

下面是读取文件的基本代码,假设每行只有一个单词。

with open('words.txt','r') as f:
    words =  f.read().split()

编辑 -- 这是带有命令行选项的版本:

import sys, os
fname = 'words.txt'
if sys.argv[1:]:
    if os.path.isfile(sys.argv[1]):
        fname = sys.argv[1] 
with open(fname,'r') as f:
    words =  f.read().split() 

撰写回答