Python:Hang Man游戏

时间:2016-11-19 23:09:29

标签: python

我正在做一个挂人游戏。我试图循环一个单词,并将信中的所有重复都附加到我的列表中。例如,单词" hello":如果用户键入" l"我希望所有的l都被添加到我的列表中。现在它只找到一个" l"如果用户键入" l"再次找到第二个" l"。

我还希望用户以前输入过的字母不能输入另一个字母。

我有两个列表,一个是正确的猜测和错误的猜测,存储每个猜测。例如,如果用户输入" h" in"你好"

" H"是一个正确的猜测所以它附加到[h]但是如果他们输入" h"再次将它添加到列表中,因此它显示[" h"," h"]。错误的框以相同的方式工作,但对于错误的单词。如果他们输入" 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))

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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!!')