首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python : Hang游戏

Python : Hang游戏
EN

Stack Overflow用户
提问于 2016-11-20 07:09:29
回答 2查看 136关注 0票数 0

我在做一个绞刑人游戏。我正在尝试在一个单词中循环,并将字母中的所有重复项添加到我的列表中。例如单词"hello":如果用户键入"l“,我希望所有的l都添加到我的列表中。现在它只找到一个"l“,如果用户再次键入"l”,它会找到第二个"l“。

我还希望用户不能键入另一个字母,如果他们以前已经键入了它。

我有两个列表,一个是正确的猜测,另一个是错误的猜测,它们存储了所有的猜测。例如,如果用户在"hello“中键入"h”

"h“是一个正确的猜测,所以它会附加到h后面,但如果他们再次输入"h",它会将其添加到列表中,所以它会说"h”,"h“。错误框的工作方式是相同的,但对于错误的单词。如果他们输入"z“作为单词"hello”,它会在错误的框中输入"z“。

下面是我的代码:

代码语言:javascript
复制
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))
EN

回答 2

Stack Overflow用户

发布于 2016-11-20 07:32:23

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-11-20 07:35:48

我不确定你的直接问题是什么,但是想一想绞刑游戏,你想让用户猜测并解析他们正在猜测的整个单词或短语,看看他们的猜测是否与单词中的任何一个匹配。我在下面做了一个hang游戏,它将按预期运行(减去任何错误处理),如果有任何部分让你感到困惑,请告诉我,我可以解释

代码语言:javascript
复制
    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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40698907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档