如何在特定游戏的列表中找到重复项?

时间:2017-06-09 17:34:54

标签: python duplicates

所以我一直在编写这个刽子手代码,除了1个问题外,它已经完成了很多工作。如果一个单词中有多个相同的字母,则只附加一个字母。如何制作它以便附加所有字母?

代码:

import getpass

hangman_pics = ['''
    +---+
        |
        |
        |
       ===''','''
    +---+
    O   |
        |
        |
       ===''','''
    +---+
    O   |
    |   |
        |
       ===''','''
    +---+
    O   |
   /|   |
        |
       ===''','''
    +---+
    O   |
   /|\  |
        |
       ===''','''
    +---+
    O   |
   /|\  |
   /    |
       ===''','''
    +---+
    O   |
   /|\  |
   / \  |
       ===''']

let_list = []
word_list = []
alreadyGuessed = []
correctGuessed = []
falseGuessed = []
statusList = []

def player_input():
    word_input = getpass.getpass("Choose the word: ")
    for letter in word_input:
        let_list.append(letter)


def get_status():
    theStar = '_'
    for x in range(len(let_list)):
        statusList.append(theStar)
    while len(let_list) < len(statusList):  
        statusList.pop()


def player_guess():
    while let_list:
        print (hangman_pics[len(falseGuessed)]) #Got this from the tutorial
        print ("Please enter your guess: ")
        guess = input()
        if guess in let_list:
            if guess in alreadyGuessed:
                print ('Oops, you already guessed ' + guess)
            else:
                alreadyGuessed.append(guess)
                correctGuessed.append(guess)
                print (guess + ' is correct!')
        else:
            alreadyGuessed.append(guess)
            falseGuessed.append(guess)
            print (guess + ' is wrong, sorry')
        while set(let_list) == set(correctGuessed):
            print ('Congratulations! You win!')
            get_status()
            word_status(guess)
            return False
        while len(falseGuessed) == len(hangman_pics):
            print ('Oh no, you killed him! You lose :(')
            get_status()
            word_status(guess)
            return False
        get_status()
        word_status(guess)


def word_status(guess):
    if guess in let_list:
        guess_index = let_list.index(guess)
        del statusList[guess_index]
        statusList.insert(guess_index,guess)
    print ("The Word: ", statusList)

def letter_dup(guess,let_list):             #working on it.
    while let_list.count(guess) > 1:
        statusList.append(guess)
        let_list.remove(guess)



player_input()
player_guess()

1 个答案:

答案 0 :(得分:1)

你的问题是你将每个字母附加到已经猜过一次,如果它在单词中,如果那个单词有很多同一个字母的实例,那么你不会让它被接受。什么是更好的解决方案是从let_list中删除字母,你根本不需要已经猜到。

但是您的代码中存在更多问题: 那些循环应该是条件 当你比较时:

set(let_list) == set(correctGuessed)

对于'abba'这个词是正确的,并且猜测'a'和'b',因为

set('abba') == set('ab')