2017-05-15 13 views
1

基本的に私はハングマンタイプのゲームを作ろうとしています。言葉にある手紙が推測されるとき、私はその手紙が次のラウンドでそこにとどまることを望みます。私はリストを使ってこれをやろうとしましたが、特定の空白を手紙に置き換える方法を見つけることができませんでした。Pythonの推測ゲームは、空白を文字で置き換えてリストに格納します。

はい、私はこのトピックに関する他の質問を読みましたが、私のために働かせることができませんでした。それを行うには、よりpytonishな方法があります

randWord = "something" 
blanks = len("something")*"_ " 
guessLetter = "t" 

print(blanks) 
if guessLetter in randWord: 
    for index, letter in enumerate(randWord, 0): 
     if guessLetter == randWord[index]: 
      temp = list(blanks) 
      temp[2*index] = guessLetter 
      blanks = "".join(temp) 
print(blanks) 

が、あなたのコードに判断すると、私はあなたにそれを表示しないことを好む:

import random 
import time 

def intro(): 
    print ('''Welcome to the word guessing game! 
    You will have 6 letter guesses to figure out my word! 
    First you must choose a topic. 
    Choose from the following: (1) Cars , (2) Diseases , 
    (3) Animals , or (4) Mathematical Words''') 

def optionSelect(): 
    choice = '' 
    while choice != '1' and choice != '2' and choice !='3' and choice !='4': 
     choice = input() 
    return choice 

def checkChoice(chosenOption): 
    if chosenOption == '1': 
     sectionOne() 
    elif chosenOption == '2': 
     sectionTwo() 
    elif chosenOption == '3': 
     sectionThree() 
    elif chosenOption == '4': 
     sectionFour() 
    else: 
     print('You didnt choose an option...') 

def sectionOne(): 
    words = ['mclaren ', 'bugatti ', 'aston martin ', 'mitsubishi '] 
    randWord = random.choice(words) 
    blanks = '_ ' * len(randWord) 
    guessing(blanks, randWord) 


def sectionTwo(): 
    words = ['gonorrhea ', 'nasopharyngeal carcinoma ', 'ependymoma ', 'tuberculosis '] 
    randWord = random.choice(words) 
    blanks = '_ ' * len(randWord) 
    guessing(blanks, randWord) 



def sectionThree(): 
    words = ['tardigrade ', 'komodo dragon ', 'bontebok ', 'ferruginous hawk '] 
    randWord = random.choice(words) 
    blanks = '_ ' * len(randWord) 
    guessing(blanks, randWord) 


def sectionFour(): 
    words = ['pythagorean ', 'differentiation ', 'polyhedron ', 'googolplex '] 
    randWord = random.choice(words) 
    blanks = '_ ' * len(randWord) 
    guessing(blanks, randWord) 


def guessing(blanks, randWord): 
    missed = [] 
    print() 
    print ("Word: ",blanks) 
    attempts = 15 
    while attempts != 0: 
     attempts = attempts - 1 
     print ('Guess a letter, you have ' + str(attempts) + ' attempts left.') 
     guessLetter = input() 
     if guessLetter in randWord: 
      newBlanks = " ".join(c if c in guessLetter else "_" for c in randWord) 
      print ('Correct!') 
      print() 
      print ("Word: ",newBlanks) 
     else: 
        missed.append(guessLetter) 
        print ('That letter is not in the word, you have guessed the following letters:') 
        print (', '.join(missed)) 
        print() 


playAgain = '' 
while playAgain != 'yes' and playAgain!= 'no': 
    intro() 
    optionNumber = optionSelect() 
    checkChoice(optionNumber) 
    print('Do you want to play again? (yes or no)') 
    #Play again or not 
    playAgain = input() 
    if playAgain == 'yes': 
     played = 1 
     playAgain ='' 
    else: 
     print('Thanks for Playing! Bye!') 
     time.sleep(2) 
     quit() 
+0

として考慮されていないことを確認したいと思うかもしれません:あなたは、ループしながら、次のように使用していることを修正することができますあなたのコードで?または少なくともそれは問題があると思いますか? –

+0

正確に何を保存したいですか?ランダムな単語、またはユーザーの試みですか? – Ajax1234

答えて

1

問題がそうするたびに、ユーザー、あなたのnewBlanksを格納しないということです正しい文字を推測すると、プログラムは文字列を再作成します。だから、どの文字が正しく推測されているかを把握する必要があります。 ifステートメントで再評価されていないローカル変数内の推測されたすべての文字を置き換えることで、そうすることができます。

while attempts != 0: 
    attempts = attempts - 1 
    print ('Guess a letter, you have ' + str(attempts) + ' attempts left.') 
    guessLetter = input() 
    if guessLetter in randWord: 
     newBlanks = " ".join(c if c in guessLetter else "_" for c in randWord) 
     index = 0 
     for letter in newBlanks: 
      if letter != '_' and letter != ' ': 
       blanks= blanks[:index] + letter + blanks[index+1:] 
      index += 1 
     print ('Correct!') 
     print() 
     print ("Word: ",blanks) 

PS、あなたの言葉のいくつかはそれらに空白を持って、あなたが働いていない、正確にどのようなスペースが推測

0

あなたはこれを試すことができます。

1

newBlanks変数を設定した行は、現在入力されている文字についてのみ心配します。推測機能で以前に推測された文字の記憶域はありません。

正常に推測された文字のリストを作成し、randWordの各文字が最後に正常に推測された文字の代わりにそのリストの文字の1つと一致するかどうかを確認できます。

私の例では、リストは「got」と呼ばれています。

def guessing(blanks, randWord): 
    missed = [] 
    got = [] #successful letters 
    print() 
    print ("Word: ",blanks) 
    attempts = 15 
    while attempts != 0: 
     attempts = attempts - 1 
     print ('Guess a letter, you have ' + str(attempts) + ' attempts left.') 
     guessLetter = input() 
     if guessLetter in randWord: 
      #add the letter to the 'got' list 
      got.append(guessLetter) 
      #newBlanks now adds in all successful letters 
      newBlanks = " ".join(c if c in got else '_' for c in randWord) 
      print ('Correct!') 
      print() 
      print ("Word: ",newBlanks) 
     else: 
      missed.append(guessLetter) 
      print ('That letter is not in the word, you have guessed the following letters:') 
      print (', '.join(missed)) 
      print() 
1

以前に推測答えを格納するクラスを使用し、メソッドとして、すべての機能を持つことができます。

class Hangman: 
    def __main__(self): 
     self.last_word = '' 

    def intro(self): 
     print ('''Welcome to the word guessing game! 
     You will have 6 letter guesses to figure out my word! 
     First you must choose a topic. 
     Choose from the following: (1) Cars , (2) Diseases , 
     (3) Animals , or (4) Mathematical Words''') 

    def optionSelect(self): 
     self.choice = '' 
     while choice != '1' and choice != '2' and choice !='3' and choice !='4': 
     self.choice = input() 
     return self.choice 

    def checkChoice(self, chosenOption): 
     if chosenOption == '1': 
      self.sectionOne() 
     elif chosenOption == '2': 
      self.sectionTwo() 
     elif chosenOption == '3': 
      self.sectionThree() 
     elif chosenOption == '4': 
      self.sectionFour() 
     else: 
      print('You didnt choose an option...') 

     def sectionOne(self): 
     self.words = ['mclaren ', 'bugatti ', 'aston martin ', 'mitsubishi '] 
     randWord = random.choice(words) 
     self.blanks = '_ ' * len(randWord) 
     self.guessing(blanks, randWord) 
    #the rest of the sections have been omitted for brevity 

     def guessing(self, blanks, randWord): 
      self.missed = [] 
      print() 
      print ("Word: ",blanks) 
      self.attempts = 15 
      while self.attempts != 0: 
      self.attempts -= 1 
      print ('Guess a letter, you have ' + str(self.attempts) + ' attempts left.') 
      self.guessLetter = input() 
      if guessLetter in randWord: 
       newBlanks = " ".join(c if c in guessLetter else "_" for c in randWord) 
       print ('Correct!') 
       print() 
       print ("Word: ",self.newBlanks) 
       self.last_word = self.newBlanks #here, we are storing the word that is correct 
      else: 
       missed.append(self.guessLetter) 
       print ('That letter is not in the word, you have guessed the following letters:') 
       print (', '.join(self.missed)) 
       self.last_word = self.missed 
       print() 

playAgain = '' 
the_game = Hangman() 
while playAgain != 'yes' and playAgain!= 'no': 

    the_game.intro() 
    optionNumber = the_game.optionSelect() 
    the_game.checkChoice(optionNumber) 
    print('Do you want to play again? (yes or no)') 
    #Play again or not 
    playAgain = input() 
    if playAgain == 'yes': 
     played = 1 
     playAgain ='' 
    else: 
     print('Thanks for Playing! Bye!') 
     time.sleep(2) 
     quit() 
関連する問題