2016-11-19 48 views
0

我正在製作一個瘋狂的男人遊戲。我正在嘗試在單詞中循環,並將信中的所有重複內容都附加到我的列表中。例如,單詞「hello」:如果用戶鍵入「l」,我希望所有的l都被添加到我的列表中。現在它只找到一個「l」,如果用戶再次鍵入「l」,它會找到第二個「l」。Python:Hang Man遊戲

我也希望用戶不能夠輸入在另一封信中,如果他們以前已經在鍵入它。

我有兩個表一個正確的猜測和存儲每一個猜測錯誤的猜測。例如,如果用戶在「你好」中鍵入「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

謝謝!我會明白的 –