2016-12-11 10 views
1

私がしようとしているのは、ユーザーからのテキストファイルを読み取り、その特定のファイルでゲームを実行することです。関数がある方がより簡単になることは知っていますが、関数を使ってコード全体を変更する方法はわかりません。ユーザーからの入力ファイルの読み込み

ユーザーが入力したテキストファイルでゲームを実行するにはどうすればよいですか?

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: ") 

答えて

1

、プログラム内の2つのミス

  1. newIn = open(fileInput).readlines() newIn=List は、第一の可変newInにファイルを読み込み、それを設定してnewInのコンテンツを削除ありますListのコンテンツ。私はあなたが2番目の部分で行ったように List = open(fileInput).readlines() のようにしたいと思います。

  2. 2番目の間違いは、ユーザーが2を押すとゲーム全体が再生されることです。 while lives != 0 and hidden_word != word: で始まるブロック全体を左に移動し、ユーザーが1または2を押したときにブロックが入力されるようにしてください。また、あなたがこれを行う場合は、プログラムの最後にelseは理解していないためwhile lives != 0...

0

まず選択肢1の場合は、フォルダ内のすべてのファイルを保持し、このファイルを実行します。..

:あなたのnew.txtファイルを与えるか、オプション2の同じファイルを使用してみてください(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: ") 
+0

申し訳前に右に行く必要がありますが、それは私のテキストファイル「words_for_hangman」を実行し、私はあなたのアドバイスに従ったが、と私新しいnew.txtファイルが動作しません。私は "newIn = List"と書かれていて、まだ定義されていないListを見ました。私はそこに何か変更を加えるべきですか? –

+0

@JohnRobertsはい、私はちょうどchoose2でこれを実行しようとしましたが、あなたのプログラムの詳細には入っていませんでした。とにかく解決策を手に入れました。良い一日 –

関連する問題