2014-10-16 62 views
0

我正在用Python 3.x創建一個hang子手遊戲。我已經創建了遊戲,可以從單詞陣列中隨機選擇一個單詞,並允許用戶使用工作生活系統進行猜測。但是我不知道該怎麼做才能讓用戶將自己的單詞添加到遊戲中。我看過其他類似的問題,但無濟於事。如何讓用戶添加單詞到hang子手遊戲

下面是代碼:

#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

也許加載從[在一個文件中的行]數組(http://stackoverflow.com/questions/3277503/python-讀文件的行由行成陣列)? – 2014-10-16 17:37:40

回答

0

這取決於你希望它如何工作。 您可以在遊戲開始的時候問,如果用戶想打,或增加新的單詞,然後你只需要添加新詞列出這樣

word=input() 
words.append(word) 

那麼你也可以,如果用戶想要問繼續或玩。 請注意,這些詞將僅在本次會議上提供。如果你想要更多的東西,你必須使用文件I/O。

1

我會這樣做的方式是讓程序從文件中加載單詞列表。您可以在腳本所在的文件夾中有一個「words.txt」文件,用戶可以使用任何文本編輯器對其進行修改。

也許你可以有一個可選的命令行參數來告訴程序使用不同的文件。

假設每行有1個字,這將是讀取文件的基本代碼。

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()