2016-11-19 16 views
0

Ich mache ein Hang-Man-Spiel. Ich versuche, durch ein Wort zu radeln und alle Wiederholungen im Brief an meine Liste anzuhängen. Zum Beispiel das Wort "Hallo": Wenn der Benutzer "l" eingibt, möchte ich, dass alle ls zu meiner Liste hinzugefügt werden. Im Moment wird nur ein "l" gefunden und wenn der Benutzer erneut ein "l" eingibt, findet er das zweite "l".Python: Hang Man Spiel

Ich möchte auch den Benutzer nicht wieder einen Brief zu schreiben in der Lage sein, wenn sie zuvor bereits es eingegeben.

Ich habe zwei Listen eine für richtige Vermutungen und falsche Vermutungen, die jede Vermutung speichern. Zum Beispiel, wenn ein Benutzer "h" in "Hallo" eintippt

"h" ist eine richtige Schätzung, so dass es an [h] hängt, aber wenn sie wieder "h" eingeben fügt es es der Liste hinzu, so heißt es ["h", "h"]. Die falsche Box funktioniert auf die gleiche Weise, aber für Wörter, die falsch sind. Wenn sie "z" für das Wort "Hallo" eingeben, steht ["z"] in der falschen Box.

Hier ist mein Code:

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

Antwort

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

Es ist nicht klar, wie Sie Eingänge nehmen, aber ich denke, dieser Code kann weiter optimiert werden. Versuch es einmal.

Edit 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

erklären ich dort mehr von meinem Code setzen. Ist das genug? –

+0

@CurrentlyVictor hinzugefügt einige weitere Vorschläge, ich hoffe, das hilft – Aditya

0

Ich bin nicht sicher, was Ihre direkte Frage, sondern um ein Hangman Spiel denken Sie die Benutzer erraten und analysieren das gesamte Wort oder eine Phrase sie erraten nehmen wollen, um zu sehen wenn ihre Schätzung irgendwo im Wort übereinstimmt. Ich habe einen Hang Mann Spiel unter diesem Willen Funktion wie erwartet (abzüglich etwaiger Fehlerbehandlung) Lassen Sie mich wissen, ob irgendwelche Teile, die Sie verwirren, und ich kann

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

Es ist wichtig, eine Liste von Leerzeichen auszugeben, so dass der Benutzer sehen und eine Vorstellung von dem Rätsel bekommen sie lösen können. – BLang

+0

Danke! Ich werde sehen –