#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项目,我在这里有一个问题:
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)
我没有从中得到错误,但它没有做我需要它做的事情。我试图让它用一个字母替换_ _ _ _ _,以防它们从_ _ _ _ _ _到_ u _ _ _ 得到正确的字母。 我认为这会解决这个问题,而不是使用dash_letter = create_hangman.dash [word_location * 2]来获取'',我会得到它的索引号。但我不知道怎么做,有什么想法吗?感谢
答案 0 :(得分: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)