Hangman从用户读取输入文件

时间:2016-12-11 13:50:58

标签: python python-3.x

我要做的是从用户中读取文本文件,然后使用该特定文件运行游戏。我知道拥有函数会更容易,但我不知道如何用函数修改整个代码。

如何使用用户输入的文本文件运行游戏?

import random
import sys

choice = None

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

Please select a menu option:

0 - Exit
1 - Enter a new text file to be read:
2 - Play Game

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

if choice == "0":
    sys.exit("Exiting from Python")
elif choice =="1":
    newIn = []
    fileInput = input("Enter a new text file name: ")
    newIn = open(fileInput).readlines()
    newIn=List

elif choice == "2":
    List = open("words_for_hangman.txt").readlines()
    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.strip()
        hidden_word = "*" * len(word)
        lives = 10
        guessed = []
    elif level == "1":
        word = random.choice(List)
        word = word.strip()
        hidden_word = "*" * len(word)
        lives = 7
        guessed = []
    elif level == "2":
        word = random.choice(List)
        word = word.strip()
        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: ")

2 个答案:

答案 0 :(得分:1)

乍一看,程序中有两个错误

  1. 线条 newIn = open(fileInput).readlines() newIn=List 首先将文件读入变量newIn,然后通过将其设置为newIn的内容来删除List的内容。我想你想做 List = open(fileInput).readlines() 正如你在第二部分所做的那样。

  2. 第二个错误是只有当用户按下2时才会播放整个游戏。尝试从开始移动整个块 while lives != 0 and hidden_word != word: 在左侧,以便在用户按下12时输入阻止。此外,如果您这样做,程序末尾的else应该在while lives != 0...

  3. 之前

答案 1 :(得分:0)

首先将所有文件保存在文件夹中并运行此文件..

选择1:提供你的new.txt文件或尝试使用选项二的相同文件。(words_for_hangman.txt)但是如果你把所有文件保存在同一个文件夹中,这将很容易。

import random
import sys

choice = None

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

    Please select a menu option:

    0 - Exit
    1 - Enter a new text file to be read:
    2 - Play Game

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

    if choice == "0":
        sys.exit("Exiting from Python")
    elif choice =="1":
        newIn = []
        fileInput = input("Enter a new text file name: ")
        newIn = open(fileInput).readlines()
        newIn=List

    elif choice == "2":
        List = open("words_for_hangman.txt").readlines()
        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.strip()
            hidden_word = "*" * len(word)
            lives = 10
            guessed = []
        elif level == "1":
            word = random.choice(List)
            word = word.strip()
            hidden_word = "*" * len(word)
            lives = 7
            guessed = []
        elif level == "2":
            word = random.choice(List)
            word = word.strip()
            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: ")