2014-12-08 93 views
1
#HangMan - 2014 
import random 
import time 

secret = "" 
dash = "" 

def create_hangman(): 
    #List of words, pick a word, then set it to a var 
    words = ["soccer", "summer", "windows", "lights", "nighttime", "desktop", "walk"] 
    d = random.randint(0, 6) 

    #Tell the compiler we want the global secret var 
    global secret 
    #Change the global secret v to a string while we choose the word 
    secret = str(words[d]) 

    #The blank spaces. Find how many letters the word is and replace it with underscores 
    create_hangman.dash = "_ " * len(secret) 

    #Print the hangman 
    print(''' 
      O 
     ----|---- 
      | 
      [|] 
      [ ] 

    ''', create_hangman.dash, secret) 

def guess(letter): 
    #If the guess is in the word... 
    if(letter in secret): 
     print("Congratulations!", letter, " was found!") 
     edit_blanks(letter) 
    else: 
     print(letter, "is not in the secret word!") 

def edit_blanks(letter): 
    #BUG: If there is more then one letter it shows -1. Make an if statement to see if there is more than one letter in it 
    #Need to find what number the letter is in the secret word 
    word_location = secret.find(letter) 
    #Now from the location of the correct word find the location in the dashs 
    dash_letter = create_hangman.dash[word_location * 2] 
    #Replace the correct dash to the letter 
    create_hangman.dash = create_hangman.dash.replace(dash_letter, letter) 
    #BUG: This replaces all _'s not just the one Ex: from _ _ _ _ _ to a a a a a when it should be _ a _ _ _ 
    print(create_hangman.dash) 

def wrong_word(letter): 
    #TODO: Make a peice on the hangman 
    print("") 

name = input("Whats your name? ") 
print("Hey", name, "welcome to HangMan 1.2") 
create_hangman() 
think = input("Pick a letter: ") 
guess(think) 

好的,所以我有上面的代码有问题。这是我的第一个Python项目,我有此位这里有一个问题:Python正在取代所有_当我只需要一个替换

def edit_blanks(letter): 
    #BUG: If there is more then one letter it shows -1. Make an if statement to see if there is more than one letter in it 
    #Need to find what number the letter is in the secret word 
    word_location = secret.find(letter) 
    #Now from the location of the correct word find the location in the dashs 
    dash_letter = create_hangman.dash[word_location * 2] #I use * 2 because there is a space _ _ 
    #Replace the correct dash to the letter 
    create_hangman.dash = create_hangman.dash.replace(dash_letter, letter) 
    #BUG: This replaces all _'s not just the one Ex: from _ _ _ _ _ to a a a a a when it should be _ a _ _ _ 
    print(create_hangman.dash) 

我不从这个得到一个错误,但它不会做什么,我需要做的。我试图用一个字母来代替_ _ _ _ _以防他们得到正确的字母,因此,_ _ _ _ _ _ _ _ _ _ _ 。 我认为会解决这个问题,而不是使用dash_letter = create_hangman.dash [word_location * 2]得到''我会得到它的索引号。但我不知道该怎么做,有什么想法?谢谢

回答

0

为什么不列出下划线,然后用索引替换它们。然后,当您必须将列表打印到控制台时,请使用''.join(list)。所以也许像这样你的删除短划线功能?我所做的只是首先创建一个与单词长度相同的空白下划线列表,然后只需为需要替换的任何字母运行remove_dash函数即可。

word = 'telephone' 
hangman_dash = ['_' for x in range(len(word))] 
def remove_dash(letter): 
    for i in range(len(word)): 
     if word[i] == letter: 
      hangman_dash[i] = letter 

remove_dash('e') 
print hangman_dash 
print ''.join(hangman_dash) 
相关问题