2016-11-19 9 views
0

私はハングマンゲームを作っています。私は言葉を繰り返し、手紙のすべての繰り返しを私のリストに追加しようとしています。たとえば「hello」という単語:ユーザーが「l」を入力すると、すべてのリストがリストに追加されます。今は1つの "l"しか見つけられません。ユーザーが "l"をもう一度入力すると、2番目の "l"が見つかります。Python:Hang Man game

私はまた、彼らは以前にすでにそれを入力した場合、ユーザは、別の文字を入力することができないようにしたい。

私は二つのリストに正しい推測とすべての推測を保存間違った推測のための1つを持っています。たとえば、ユーザーが「hello」で「h」を入力した場合

「h」は正しい推測であるため、[h]に追加されますが、「h」にもう一度入力するとリストに追加されます["h"、 "h"]。間違ったボックスは、同じ方法で動作しますが、間違った単語に対しては動作します。 "hello"という単語に "z"を入力すると、間違ったボックスに["z"]と表示されます。ここで

は私のコードです:

import random 

user_input = "" 

turns = 5 

print("Welcome to Advanced Hang Man!") 

print("Use your brain to unscramble the word without seeing its order!") 

words = ["hello","goolge","czar","gnat","relationship","victor","patric","gir","foo","cheese"] 

# Picks a random word from the list and prints the length of it 
random_word = (random.choice(words)) 

random_word_legnth = (len(random_word)) 

print("Hint! The length of the word is",random_word_legnth) 

hold_random_word = [i for i in random_word]  

while turns != 0 and set(right_guess) != set(hold_random_word): 

user_input = input("Please type your guess one letter at a time:") 

right_guess = [] 
wrong_guess = [] 

#Calculating every input 
if len(user_input) == 1 and user_input.isalpha(): 
    for i in user_input: 
     if i in hold_random_word: 
      right_guess.append(i) 
     else: 
      wrong_guess.append(i) 

    print("Correct guess", ''.join(right_guess)) 
    print("Wrong guess", ''.join(wrong_guess)) 

答えて

0
if len(user_input) == 1 and user_input.isalpha(): 
    for i in user_input: 
     if i in hold_random_word and i not in right_guess: 
      right_guess.append(i) 
     elif i not in hold_random_word or i not in wrong_guess: 
      wrong_guess.append(i) 
     elif i in hold_random_word: 
      # here user types something that is already typed and is a right_guess 
      pass 
     else: 
      # Types something wrong, that was already typed 
      pass 

    print("Correct guess", ''.join(right_guess)) 
    print("Wrong guess", ''.join(wrong_guess)) 

あなたが入力を取っているかは明らかではないが、私はこのコードをさらに最適化することができると思います。試してみます。

編集1:

import random 

user_input = "" 

turns = 5 

print("Welcome to Advanced Hang Man!") 

print("Use your brain to unscramble the word without seeing its order!") 

words = ["hello","goolge","czar","gnat","relationship","victor","patric","gir","foo","cheese"] 

random_word = (random.choice(words)) 

random_word_legnth = (len(random_word)) 

print("Hint! The length of the word is",random_word_legnth) 

hold_random_word = [i for i in random_word] 

# This condition can lead to issues in situations like this - abc and aabbcc [sorry couldn't quickly come up with a good actual example :)] 
while turns != 0 and set(right_guess) != set(hold_random_word): 

    user_input = input("Please type your guess one letter at a time:").strip() 

    right_guess = [] 
    wrong_guess = [] 

    #Calculating every input 
    if len(user_input) == 1 and user_input.isalpha(): 
     # user_input is 1 letter so for i in user_input will execute only once 
     # Use the if structure as defined above 
     if user_input in hold_random_word: 
      right_guess.append(i) 
     else: 
      # this is missing 
      turns -= 1 
      wrong_guess.append(i) 
     print("Correct guess", ''.join(right_guess)) 
     print("Wrong guess", ''.join(wrong_guess)) 
    elif len(user_input) > 1: 
     print("Please type only one letter at a time") 
    elif not user_input.isalpha(): 
     print("Please enter only valid English letters") 
    else: 
     # handle this however you want :) 
     pass 
+0

を説明することができます。それで十分ですか? –

+0

@CurrentlyVictorはいくつかの提案を追加しました。私はこれが助けてくれることを願っています。 – Aditya

0

私はあなたの直接の質問が何であるかわからないんだけど、あなたは、ユーザーが推測取ると、彼らは見ることが推測されている全体の単語やフレーズを解析するハングマンゲームを考えます彼らの推測が単語​​のどこにでも一致する場合。私は予想通りその意志機能の下にハング男のゲームを作った(マイナス任意のエラー処理)いずれかの部品があなたを混同している場合、私に教えてください、と私はそこに私のコードの多くを置く

import random 
    wordcomp = [] 
    wordguess = [] 
#this is a word bank for all puzzles, they are randomly chosen 
    word_bank = ['guess this phrase', 'Lagrange', 'foo', 'another phrase to guess'] 
    # This loop stores each letter in a list, and generates a blank list with correct spaces and blanks for user 
    rand_word = random.randrange(4) 
    for i in word_bank[rand_word]: 
     wordcomp.append(i) 
     if i == ' ': 
      wordguess.append(' ') 
     else: 
      wordguess.append('__') 
    print('I am thinking of a word,' , wordguess , ' it has ', len(wordguess), ' characters total, GOOD LUCK \n') 
    wordlist = wordcomp  
    count = 0 
    placeletter = 0 
    wrongguess = [] 
    guesscount = 0 
    while wordlist != wordguess: 
     guess = input('please input a lower case letter within the english alphabet!') ##Check that input is one character, and lower case 
     guesscount = guesscount + 1 
     # This for loop scans through to see if the letter that was guessed is in the actual puzzle, and places in the correct spot!! 
     for t in wordcomp: 
      if t == guess: 
       wordguess[placeletter] = guess 
      placeletter = placeletter + 1 
     # This check tells them they already guessed that letter... then makes fun of them 
     if guess in wordguess: 
      pass 
     else: 
      wrongguess.append(guess) 
      while wrongguess.count(guess) > 1: 
       wrongguess.remove(guess) 
       print('you guessed the letter ' , guess , ' already, are you person that suffers short term memory loss...') 
     print('The word I am thinking of: ' , wordguess) 
     print('The letters you have already guess are: ', wrongguess) 
     placeletter = 0 
    # This tells them they finished the puzzle and the number of guesses it took, if its over 26, it calls them stupid for obvious reasons... 
    if guesscount >= 26: 
     print('you are an idiot if it took you more than 26 guesses..... now take a minute, sit silently, and think about why you are idiot if it took over 26 guesses... for hangman... where you guess the letters of the alphabet... YOU GET IT, stupid') 
    elif guesscount < 26: 
     print('Congrats you solved the puzzle, w00t!!') 
+0

空白のリストを出力することが重要なので、ユーザーは解決しているパズルのアイデアを確認できます。 – BLang

+0

ありがとう!私は見るよ –