简体   繁体   English

我如何让我的python hangman游戏的用户输入他们自己的单词?

[英]How can I let the users of my python hangman game input their own words?

I have been stuck on this for a while now and I can't figure out what to do. 我已经被困在这一段时间了,我无法弄清楚该怎么做。 I know that it will probably involve creating an empty list and then adding the words to the list but I haven't got a clue when it comes to retrieving the words as I have never done this before. 我知道它可能会涉及创建一个空列表,然后将这些单词添加到列表中,但是在检索单词时我还没有得到线索,因为我之前从未这样做过。 the code itself works, it's just adding this new function I'm having trouble with. 代码本身有效,它只是添加了我遇到麻烦的新功能。

import random
import sys 
#importing both random and time modules

print("Hello!")
playAgn = 'yes'
animalNames = ['wildebeest','wolverine','woodlouse','woodpecker','yak','zebra']
#this is the list of words that the user will have to guess if they play the game multiple times
while playAgn == 'yes':
    secretWord = random.choice(animalNames)
    #this line tells the computer to randomly select one of the words in the list "animalNames"
    lives = 6
    #Defining how many lives the user has
    guessedletters = []

    print("Okay!\n Lets play hangman!!")
    while lives > 0:
        inword = False
        #user has not guessed word yet so the boolean value is automatically off (False)
        print("you have",lives, "lives")
        #tells the user how many lives they have left 
        guessedletter = input("Guess a letter: ")
        if len(guessedletter) > 1:
            print("Error! Please only enter 1 letter at a time")
            #if the user tries to guess something longer than 1 character the computer displays a message asking to only enter 1 letter at a time
        elif guessedletter in guessedletters:
            print("Sorry, you have already guessed this letter. Try again")
            #if the user tries to guess a letter that has already been guessed the computer displays a message telling the user to choose another letter as they have already chose this one

        else:
            guessedletters+=str(guessedletter)
            #adds the guessed letter to guessedletters variable
            print("You have guessed:")
            for letter in guessedletters:
                print(letter.upper(),"")
                #prints the letters already guessed in uppercase
            shownword = ""
            for letter in secretWord:
                if letter in guessedletters:
                    shownword +=str(letter)
                    #if the letter is in guessedletters then add the letter to shownword(word displayed to user)
                    if letter == guessedletter:
                        inword = True
                        #inword is true as the guessed letter is in the secret word

                else:
                    shownword+=str("_")
                    #the computer is now adding the underscores too the word being displayed to the user
            print(shownword)
            #the computer prints the word with the letters that the user has guessed so far (word including underscores for not yet guessed characters)
            if "_" not in shownword:
                        print("Congratulations, you won! The word was '", shownword,"'")
                        #if there are no underscores(meaning the word is completed) tell the user they have won the game
                        break
            elif inword == False:
                #the guessed word is not in the secret word so the boolean value is now off (False)
                print("No luck," , guessedletter , "is not in my word")
                lives -= 1
                #deducts a life for the wrong letter being guessed and also displays a message telling the user that the letter just guessed isn't in the secret word


    if lives == 0:
        print("You have run out of lives. The word was '",secretWord,"'")
        #if the user runs out of lives and still hasn't guessed the word, tell them they failed to beat the game and also tell them the word

    while True:
        playAgn = input("Would you like to play again? yes/no: ")
        if playAgn == 'no':
            print("okay\n Goodbye!")
            break
        elif playAgn == 'yes':
            break

thanks a lot to anyone who can help figure this out :) 非常感谢任何能帮助解决这个问题的人:)

You could just swap this: 你可以交换这个:

animalNames = ['wildebeest','wolverine','woodlouse','woodpecker','yak','zebra']

For this: 为了这:

animalNames = getNames()

And define the function: 并定义功能:

def getNames():
    names = []
    while True:
        name = raw_input("Add a word to the list (or press enter to cancel): ")
        if name == "":
            break
        else:
            names.append(name)
    return names

Look at this line: 看看这一行:

animal_names.append(input("Enter Word To Word Pool:"))

like that? 像那样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM