2014-02-07 36 views
0

我正在製作一個名爲hangman的遊戲。我使用了一個for循環來測試祕密詞中是否有任何字母,然後在頂部打印正確的位置/他們在單詞中的字母/字母。例如,如果祕密詞是「按鈕」並且你猜到了「t」,它會打印出「_ _ tt _ _」,如果祕密詞是「貓」並且你猜「a」,它會打印出「_ a _ 」。我希望它能夠打印兩個字母都在1行中的位置,但它會打印出多行:「_ _ t _ _ _」,並在第二行顯示同樣的內容。我知道它爲什麼打印多行。我仔細看了一下。但是我需要知道如何像第一個「按鈕單詞示例」一樣在1行上打印它。它也會打印錯誤的「_」。它從來沒有給出一個成功/失敗的信息 - 我懷疑這是因爲它可能無法擺脫while循環?Python:製作hang子手。字符串故障

無論如何,幫助表示讚賞,如果您可以提供任何編程技巧和方法來整理/更好地使我的代碼請做。這是代碼;我不知道這是否有足夠的信息給任何人提供幫助,所以如果你需要更多請詢問。

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上運行。

回答

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 
+0

@ gogobebe2條從字符串http://docs.python.org/2/library/stdtypes.html#str的端部去除的空白.strip在這種情況下,它會從每行末尾刪除換行符 –

+0

@ gogobebe2是的,將它放在for循環中應該有所幫助。如果你發現這個答案有幫助,請將其標記爲已接受的答案:) –

+0

謝謝你這麼多! – gogobebe2

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***'