2016-12-10 3 views
0

В моем текстовом файле есть слова по одной строке, например, «CLASS», а затем в другую строку «PYTHON» и т. Д. Предположим, что мое случайное слово - «КЛАСС», вместо «*****» я получаю «******» с еще одним «*». Я действительно не знаю, почему ...Ошибка Hangman при чтении файла

import random 
import sys 

choice = None 
List = open("words_for_hangman.txt").readlines() 



while choice != "0": 
    print(''' 
-------------------- 
Welcome to Hangman 
-------------------- 

Please select a menu option: 

0 - Exit 
1 - Enter a new list of words 
2 - Play Game 

''') 
choice= input("Enter you choice: ") 

if choice == "0": 
    sys.exit("Exiting from Python") 
elif choice =="1": 
    list = [] 
    x = 0 
    while x != 5: 
     word = str(input("Enter a new word to put in the list: ")) 
     list.append(word) 
     word = word.upper() 
     x += 1 
elif choice == "2": 
    print(''' 

Now select your difficulty level: 

0 - EASY 
1 - INTERMEDIATE 
2 - HARD 

''') 
    level= input("Enter your choice: ") 
    if level == "0": 
     word = random.choice(List) 
     word = word.upper() 
     hidden_word = "*" * len(word) 
     lives = 10 
     guessed = [] 
    elif level == "1": 
     word = random.choice(List) 
     #word = word.upper() 
     hidden_word = "*" * len(word) 
     lives = 7 
     guessed = [] 
    elif level == "2": 
     word = random.choice(List) 
     #word = word.upper() 
     hidden_word = "*" * len(word) 
     lives = 5 
     guessed = [] 

    while lives != 0 and hidden_word != word: 
     print("\n-------------------------------") 
     print("The word is") 
     print(hidden_word.replace("_"," _ ")) 
     print("\nThere are", len(word), "letters in this word") 
     print("So far the letters you have guessed are: ") 
     print(' '.join(guessed)) 
     print("\n You have", lives,"lives remaining") 
     guess = input("\n Guess a letter: \n") 
     guess = guess.upper() 
     if len(guess) > 1: 
      guess = input("\n You can only guess one letter at a time!\n Try again: ") 
      guess = guess.upper() 
     elif guess== " ": 
      guess = input("\n You need to input a letter, not a space!\n Come on let's try again: ") 
      guess = guess.upper() 
     while guess in guessed: 
      print("\n You have already guessed that letter!") 
      guess = input("\n Please take another guess: ") 
      guess = guess.upper() 
     guessed.append(guess) 
     if guess in word: 
      print('''------------------------------- 
      ''') 
      print("Well done!", guess.upper(),"is in the word") 
      word_so_far = "" 
      for i in range (len(word)): 
       if guess == str(word[i]): 
        word_so_far += guess 
       else: 
        word_so_far += hidden_word[i] 
      hidden_word = word_so_far 
     else: 
      print('''------------------------------- 
      ''') 
      print("Sorry, but", guess, "is not in the word") 
      lives -= 1 

    if lives == 0: 
      print("GAME OVER! You have no lives left") 
    else: 
     print("\n CONGRATULATIONS! You have guessed the word") 
     print("The word was", word) 
     print("\nThank you for playing Hangman") 
else: 
    choice = print("\n That is not a valid option! Please try again!") 
    choice = input("Choice: ") 

ответ

1

Возможно, у вас есть символ новой строки, переводящий каждое слово, считанное из текстового файла, например.

'CLASS\n' 

Чтобы удалить этот дополнительный символ, который вы можете использовать strip

word = word.strip() 
Смежные вопросы