dashes = ""
for i in word_chosen:
dashes = dashes+"_ "
def get_guess():
guess = ""
guess = input("Please can you enter a guess for hangman: ")
return guess
def check_guess(dashes):
if len(guess)>1:
print("word")
else:
if guess in word_chosen:
print("The word contains an ",guess)
# Yes option
dashes_tem = " "
count = 0
# new variable made
for i in word_chosen:
# loop through the word chosen
if i == guess:# check word against guess
dashes_tem = dashes_tem + i + " "
else:
dashes_tem = dashes_tem + dashes[(count*2)] + " "
count = count + 1
return dashes_tem
else:
print("The word doesn't contain an",guess)
# No option - will come back
guess = get_guess()
dashes = check_guess(dashes)
print(dashes)
guess = get_guess()
dashes = check_guess(dashes)
print(dashes)
所以我已经制作了我的刽子手代码,当有两个正确的猜测时我正在编码它,但是当有两个正确的猜测时,它只会说这个词包含' x'但不会像第一次正确猜测那样取代下划线。
答案 0 :(得分:0)
我建议从头开始并使用能更好地映射到问题的数据结构。
例如:
将程序逻辑从用户输入中分离出来,将逻辑分解为尽可能重用的组件。例如,如果你有一个功能:
if hide_unguessed(real_word, guessed_letters) == real_word:
print "You win!"
...然后你可以这样做:
def check_guess(real_word, guessed_letter):
return guessed_letter in real_word
def hide_unguessed(real_word, guessed_letters):
return ''.join([letter if letter in guessed_letters else '_' for letter in real_word])
... 在之外的那个功能,而不是把它放进去。
您可以用作构建块的函数可能如下所示:
{{1}}
请注意,这些是纯功能 - 如果你知道他们的输入是什么,你就会总是知道他们的回报价值是什么(他们不依赖于不可预知的东西,比如用户输入),除了返回值之外,他们没有任何副作用。希望尽可能使用纯函数 - 这些比其他代码更容易推理和测试。