做了一个刽子手,不确定是否有更好的编写方法,但我是Python的新手,所以这就是我想到的。
当我有一个单词要猜测时,例如编程,我如何允许代码找到单词中的第二个"m"并将其放在单词模板中
word_selection = ("python", "coding", "language", "programming")
selected_word = random.choice(word_selection)
print(selected_word)
def hangman_game(word, guess_limit, guess_count):
letter_template = len(word) * " _"
fail_count = 0
while guess_count < guess_limit:
print("")
letter_guess = input("Guess a letter: ").lower()
if len(letter_guess) > 1:
print("Please enter 1 character/ Letter")
elif letter_guess in letter_template:
print("You already guessed this letter.")
elif letter_guess in word:
print(letter_guess, " is in the word")
letter_index = word.index(letter_guess)
letter_template = letter_template[:letter_index*2] + " " + letter_guess + letter_template[(letter_index + 1)*2:]
print(letter_template)
guess_count += 1
else:
print(letter_guess, " is not in the word")
fail_count += 1
if fail_count == 1:
print("""
_________
""")
if fail_count == 2:
print("""
|
|
|_________
""")
if fail_count == 3:
print("""
|
|
|
|
|
|
|_________
""")
if fail_count == 4:
print("""
______
|
|
|
|
|
|
|_________
""")
if fail_count == 5:
print("""
_______
| |
|
|
|
|
|
|_________
""")
if fail_count == 6:
print("""
_______
| |
| O
|
|
|
|
|_________
""")
if fail_count == 7:
print("""
_______
| |
| O
| -|-
|
|
|
|_________
""")
if fail_count == 8:
print("""
_______
| |
| O
| -|-
| /
|
|
|_________
""")
if fail_count == 9:
print ("""
_______
| |
| O
| -|-
| /|
|
|
|_________
""")
if fail_count == 9:
print("Out of guesses! The man has been hung.")
print("")
print("The word was", word)
break
else:
print("You guessed the word")
hangman_game(selected_word, len(selected_word), 0)
而不是:
print(letter_template.replace(letter_template[letter_index], letter_guess))
要从某个索引中获取字符,请使用:
letter_template =letter_template[:letter_index*2] + " "+letter_guess + letter_template[(letter_index + 1)*2:]
您的代码:
def hangman_game(word, guess_limit, guess_count, letter_guess, out_of_guesses):
letter_template = len(word) * " _" # this part should be outside of the while loop becouse you will define empty pattern every itteration
if letter_guess != word and not out_of_guesses:
while guess_count < guess_limit:
letter_guess = input("Guess a letter: ")
if letter_guess in word:
print(letter_guess, " is in the word")
letter_index = word.index(letter_guess)
#print(letter_template.replace(letter_template[letter_index], letter_guess))
letter_template =letter_template[:letter_index*2] + " " + letter_guess + letter_template[(letter_index + 1)*2:]
print(letter_template)
guess_count += 1
else:
print(letter_guess, " is not in the word")
hangman_game("python", 5, 1, "", False)
输出:
Guess a letter: t
t is in the word
_ _ t _ _ _
Guess a letter: h
h is in the word
_ _ t h _ _
Guess a letter: p
p is in the word
p _ t h _ _
Guess a letter:
编辑:
def hangman_game(word, guess_limit, guess_count, letter_guess, out_of_guesses):
letter_template = len(word) * " _" # this part should be outside of the while loop becouse you will define empty pattern every itteration
fail_count = 0
if letter_guess != word and not out_of_guesses:
while guess_count < guess_limit:
letter_guess = input("Guess a letter: ")
if letter_guess in word:
print(letter_guess, " is in the word")
letter_index = word.index(letter_guess)
#print(letter_template.replace(letter_template[letter_index], letter_guess))
letter_template =letter_template[:letter_index*2] + " " + letter_guess + letter_template[(letter_index + 1)*2:]
print(letter_template)
guess_count += 1
else:
print(letter_guess, " is not in the word")
fail_count += 1
if fail_count == 5:
print("You missed the letter 5 times! Game Over!")
return
else:
print ("You guessed the word")
hangman_game("python", len("python"(, 0, ", False(
输出:
Guess a letter: p
p is in the word
p _ _ _ _ _
Guess a letter: o
o is in the word
p _ _ _ o _
Guess a letter: a
a is not in the word
Guess a letter: a
a is not in the word
Guess a letter: a
a is not in the word
Guess a letter: a
a is not in the word
Guess a letter: a
a is not in the word
You missed the letter 5 times! Game Over!