Reputation: 3
I am creating a hangman game with Python 3.x. I have created the game up to the point where it can select a word randomly from the array of words and allow the user to guess it, with a working life system. However I have no clue on what to do to be able to allow the user to add their own set of words to the game. I have looked at other similar questions, to no avail.
Here is the code:
#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.")
Can someone please help me out on how to write out the code for this new feature I want to add and how it works?
Upvotes: 0
Views: 1919
Reputation: 995
The way I'd do it is have the program load the list of words from a file. You can have a file "words.txt" in the same folder as the script, and the user can modify it using any text editor.
And maybe you could have an optional command line argument that would tell the program to use a different file.
This would be the basic code to read the file assuming you have 1 word per line.
with open('words.txt','r') as f:
words = f.read().split()
EDIT -- Here's the version with command line option:
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()
Upvotes: 1
Reputation: 2238
It depends on how would you want it to work. You can at the beginning of the game ask if user wants to play, or add new words, and then you need only to add new word to list like that
word=input()
words.append(word)
then you can also ask if user wants to continue or play. note that those words would be available only in this session. If you would want something more pernament, you have to play around with file I/O.
Upvotes: 0