<p>必须将新单词传递给函数get\u word才能在那里使用它。当你替换的时候,你只是在创建一个新的变量new\u word。你知道吗</p>
<p>您可以:</p>
<pre><code>def get_word(random_word, new_word, letter)
</code></pre>
<p>(别忘了在hangman方法中再次将此返回属性归于新单词)</p>
<p>或<br/>
宣布为全球(不太好):
在顶部:</p>
<pre><code>new_word = ""
</code></pre>
<p>在您使用的函数中:</p>
<pre><code>global new_word
</code></pre>
<p>要求的完整代码:</p>
<pre><code>import random
words=['''list of greek words''']
greek_alphabet=['''list of all greek letters''']
def shape(left=5):
if left==5:
print('| |')
print('| O')
print('|')
print('|')
print('|')
print('|')
print('|')
elif left==4:
print('| |')
print('| O')
print('| /|\\')
print('|')
print('|')
print('|')
print('|')
elif left==3:
print('| |')
print('| O')
print('| /|\\')
print('| |')
print('|')
print('|')
print('|')
elif left==2:
print('| |')
print('| O')
print('| /|\\')
print('| |')
print('| _/ \\_')
print('|')
print('|')
elif left==1:
print('| |')
print('| O')
print('| /|\\')
print('| |')
print('| _/ \\_')
print('| ## ##')
print('|')
else:
print('| |')
print('| O')
print('| /|\\')
print('| |')
print('| _/ \\_')
print('| ## ##')
print('| fire')
def get_word(random_word, new_word, letter):
for i in random_word:
if i==letter:
new_word.replace('_ ',letter)
return new_word
def hangman():
found=False
random_word=random.choice(words)
words.remove(random_word)
used_letters=[]
max_guesses=0
incorrect_guesses=0
new_word=len(random_word)*'_ '
print('You can make up to 5 mistakes')
print('The 6th mistake is going to get you out of the game')
print('The word you must guess is: ',new_word)
while not found and incorrect_guesses<max_guesses:
letter=input('Give letter: ')
if found_hidden_word(random_word,new_word):
print('Congrats! You found the word!')
found=True
elif letter in random_word and letter not in used_letters:
used_letters.append(letter)
new_word = get_word(random_word, new_word, letter)
print('The word you must guess is', new_word)
elif letter not in greek_alphabet:
print('You did not give a letter. Try again.')
elif letter in used_letters:
print('This letter has already been used')
else:
incorrect_guesses+=1
left=max_guesses-incorrect_guesses
shape(left)
print('You still have ',max_guesses-incorrect_guesses,'lives')
new_word = get_word(random_word,letter)
print('The word you must choose is ',new_word)
if not found:
shape()
print('You did not find the word')
print('The word we were looking for was ',random_word)
return False
else:
return True
def found_hidden_word(random_word,new_word):
if new_word==random_word:
return True
hangman()
</code></pre>