Python:制作刽子手。麻烦的字符串

时间:2014-02-07 04:38:06

标签: python string

我正在做一个叫做刽子手的游戏。我用一个for循环来测试密码中是否有任何一个字母,然后在顶部打印正确/它们在单词中的字母/字母。例如,如果密码是“按钮”而你猜对了“t”,它会打印“_ _ tt _ _”,如果密码是“猫”而你猜对了“a”,它会打印“_ a _” ”。我希望它能够打印两个字母在1行中的位置,但它打印出多行:“_ _ t _ _ _”,在第二行打印相同的东西。我知道为什么它打印多行。我看了一下。但我需要知道如何在第一行打印所有内容,就像第一个“按钮字示例”一样。它还会打印错误的“_”数量。并且它永远不会给出获胜/失败的消息 - 我怀疑这是因为它可能无法摆脱while循环?

无论如何,我们非常感谢您的帮助,如果您能提供任何编程技巧和方法来整理/使我的代码更好,请做。这是代码;我不知道这是否足以让任何人给予帮助,所以如果你需要更多,请询问。

import random

file = open('LIST_OF_WORDS.txt', 'r')
word_list = file.readlines()
file.close()

alphabet = "A B C D E F G \nH I J K L M N \nO P Q R S T U \nV W X Y Z"


alphabet_dict = {"A":0, "B":2, "C":4, "D":6,"E":8,"F":10,"G":12,"H":15, \
               "I":17, "J":19, "K":21, "L":23,"M":25,"N":27,"O":30,"P":32, \
               "Q":34, "R":36, "S":38, "T":40,"U":42,"V":45,"W":47,"X":49, \
               "Y":51, "Z":53}

MAN_NOTHING = "\n\n\n\n\n\n\n\n\n\n\n"

MAN_HEAD = \
r"""
        _
       /-\
       \_/








"""

MAN_BODY = \
r"""
        _
       /-\
       \_/
      /\Y/\
      | : |
      | : |





"""

MAN_LEFT_ARM = \
r"""
        _
       /-\
       \_/
      /\Y/\
     || : |
     || : |
     (




"""

MAN_RIGHT_ARM = \
r"""
        _
       /-\
       \_/
      /\Y/\ ;-,
     || : |\//
     || : |\/
     (



"""

MAN_LEGS = \
r"""
        _
       /-\
       \_/
      /\Y/\ ;-,
     || : |\//
     || : |\/
     (|---|
      | | |
      | | |
      |_|_|

"""      

MAN_COMPLETE = \
r"""
        _
       /-\
       \_/
      /\Y/\ ;-,
     || : |\//
     || : |\/
     (|---|
      | | |
      | | |
      |_|_|
      (/ \)
"""      

def man_state(state=0):
    if state == 0:
        return MAN_NOTHING
    elif state == 1:
        return MAN_HEAD    
    elif state == 2:
        return MAN_BODY
    elif state == 3:
        return MAN_LEFT_ARM
    elif state == 4:
        return MAN_RIGHT_ARM
    elif state == 5:
        return MAN_LEGS
    elif state == 6:
        return MAN_COMPLETE

def hangman():
    num_letters_wrong = 0
    num_letters_correct = 0
    fin = False
    secret_word = random.choice(word_list)
    word_correct = None
    word_length = len(secret_word)
    letter_complete_status = list(" _" * word_length)
    while fin == False:
        print("The word was " + secret_word) #test
        print(man_state(state=num_letters_wrong))
        print(alphabet)
        letter_guess = input("\nGuess a letter: ")
        if letter_guess not in secret_word:
            if num_letters_wrong == 7:
                fin == True
                word_correct = False
            elif num_letters_wrong < 7:
                num_letters_wrong += 1
                print("".join(letter_complete_status))
            else:
                print("Error? - PLEASE! Report this to William/gogobebe2!! Thankyou :)")
        elif letter_guess in secret_word:
            num_letters_correct += 1
            if num_letters_correct >= word_length:
                fin == True
                word_correct = True
            for l in secret_word:
                if letter_guess == l:   
                    letter_complete_status[secret_word.index(l) + 1] = l
                    print("".join(letter_complete_status))
    print("The word was " + secret_word)
    if word_correct == True:
        print("Welldone, you guessed correctly")
    elif word_correct == False:
        print("Gameover! You loose!!")
        hangman()


#If the file is the main file then start the program    
if __name__ == '__main__':
    hangman()

我还应该指出我在Linux上运行。

2 个答案:

答案 0 :(得分:1)

当我运行该程序时,它似乎可以跟踪猜测,并在一行上打印已知字母:

Guess a letter: s
 _ is_ _ _
The word was this

在生成letter_complete_status

时插入空格时打印有点奇怪
letter_complete_status = list(" _" * word_length)

但是,当您覆盖这些值时,只需将1添加到索引

letter_complete_status[secret_word.index(l) + 1] = l

当你应该乘以2时

letter_complete_status[secret_word.index(l)*2 + 1] = l

我还必须从单词列表中删除额外的尾随换行符

file = open('LIST_OF_WORDS.txt', 'r')
word_list = [l.strip() for l in file]

对于具有相同字母多次的字词,您的“获胜”条件不正确

num_letters_correct += 1
if num_letters_correct >= word_length: # doesn't work for eg 'hello'

最后,您的退出条件永远不会得到满足,因为您没有正确分配fin的值

fin == True # double equals means this is just a comparison

应该只是

fin = True

答案 1 :(得分:1)

temp_word = 'correct'
letters_guessed = ['g','r','a','b']

for letter in temp_word:
    if letter not in letters_guessed:
        temp_word = temp_word.replace(letter,'_')

print temp_word

## >>> '__rr___'
## string.replace(old, new) you can use any character for new '*' returns '**rr***'