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]得到''我會得到它的索引號。但我不知道該怎麼做,有什麼想法?謝謝