Python 刽子手任务蟒蛇
为了完成任务,我需要写一个基本的刽子手游戏。除了这一部分,一切都正常 游戏应该为神秘单词中的每一个字母都打印一个下划线(389;);然后,当用户猜出(正确的)字母时,它们就会被输入 例如 假设这个词是“单词”Python 刽子手任务蟒蛇,python,Python,为了完成任务,我需要写一个基本的刽子手游戏。除了这一部分,一切都正常 游戏应该为神秘单词中的每一个字母都打印一个下划线(389;);然后,当用户猜出(正确的)字母时,它们就会被输入 例如 假设这个词是“单词” 用户猜测“W” W_uu_ 用户猜测“D” W_uud 但是,在许多情况下,一旦用户进行了一些猜测,就会丢失一些下划线,因此最终看起来像: W_D 而不是: W_uud 我不知道是哪部分代码导致了这种情况。任何帮助都将不胜感激!干杯 这是我的密码: import random choi
用户猜测“W” W_uu_ 用户猜测“D” W_uud 但是,在许多情况下,一旦用户进行了一些猜测,就会丢失一些下划线,因此最终看起来像: W_D 而不是: W_uud 我不知道是哪部分代码导致了这种情况。任何帮助都将不胜感激!干杯 这是我的密码:
import random
choice = None
list = ["HANGMAN", "ASSIGNEMENT", "PYTHON", "SCHOOL", "PROGRAMMING", "CODING", "CHALLENGE"]
while choice != "0":
print('''
******************
Welcome to Hangman
******************
Please select a menu option:
0 - Exit
1 - Enter a new list of words
2 - Play Game
''')
choice= input("Enter you choice: ")
if choice == "0":
print("Exiting the program...")
elif choice =="1":
list = []
x = 0
while x != 5:
word = str(input("Enter a new word to put in the list: "))
list.append(word)
word = word.upper()
x += 1
elif choice == "2":
word = random.choice(list)
word = word.upper()
hidden_word = " _ " * len(word)
lives = 6
guessed = []
while lives != 0 and hidden_word != word:
print("\n******************************")
print("The word is")
print(hidden_word)
print("\nThere are", len(word), "letters in this word")
print("So far the letters you have guessed are: ")
print(' '.join(guessed))
print("\n You have", lives,"lives remaining")
guess = input("\n Guess a letter: \n")
guess = guess.upper()
if len(guess) > 1:
guess = input("\n You can only guess one letter at a time!\n Try again: ")
guess = guess.upper()
while guess in guessed:
print("\n You have already guessed that letter!")
guess = input("\n Please take another guess: ")
guess = guess.upper()
guessed.append(guess)
if guess in word:
print("*******************************")
print("Well done!", guess.upper(),"is in the word")
word_so_far = ""
for i in range (len(word)):
if guess == str(word[i]):
word_so_far += guess
else:
word_so_far += hidden_word[i]
hidden_word = word_so_far
else:
print("************************")
print("Sorry, but", guess, "is not in the word")
lives -= 1
if lives == 0:
print("GAME OVER! You ahve no lives left")
else:
print("\n CONGRATULATIONS! You have guessed the word")
print("The word was", word)
print("\nThank you for playing Hangman")
else:
choice = input("\n That is not a valid option! Please try again!\n Choice: ")
您有
hidden\u word=“\u”*len(word)
这意味着在两个字母单词的开头,您有[space][下划线][space][space][space][下划线][space]
然后执行word\u so\u far+=hidden\u word[i]
,对于i=0,将附加空格,而不是下划线
最快的解决办法似乎是:
hidden\u word=“\u”*len(word)
)hidden\u单词。替换(“\u”,“\u”)
在后面的下划线周围添加空格您有
hidden\u word=“\u”*len(word)
这意味着在两个字母单词的开头,您有[space][下划线][space][space][space][下划线][space]
然后执行word\u so\u far+=hidden\u word[i]
,对于i=0,将附加空格,而不是下划线
最快的解决办法似乎是:
hidden\u word=“\u”*len(word)
)hidden\u单词。替换(“\u”,“\u”)
在后面的下划线周围添加空格@Foon向您展示了解决方案的问题 如果您可以将代码划分为一些小的功能块,那么就可以更容易地专注于一项任务,并更容易进行测试。当您在特定任务中遇到问题时,通过将问题转化为函数,可以帮助您隔离问题 像这样的
word = '12345'
guesses = ['1', '5', '9', '0']
def hidden_word(word, guesses):
hidden = ''
for character in word:
hidden += character if character in guesses else ' _ '
return hidden
print(hidden_word(word, guesses))
guesses.append('3')
print(hidden_word(word, guesses))
@Foon向您展示了解决方案的问题 如果您可以将代码划分为一些小的功能块,那么就可以更容易地专注于一项任务,并更容易进行测试。当您在特定任务中遇到问题时,通过将问题转化为函数,可以帮助您隔离问题 像这样的
word = '12345'
guesses = ['1', '5', '9', '0']
def hidden_word(word, guesses):
hidden = ''
for character in word:
hidden += character if character in guesses else ' _ '
return hidden
print(hidden_word(word, guesses))
guesses.append('3')
print(hidden_word(word, guesses))
下面的代码解决了此问题。您可以根据需要进行一些修改。如果单词中存在猜测的字母,则该字母将添加到显示变量中。如果不存在,则可以发出警告。但请注意,这可能会诱使您在for循环中编写ELSE语句(条件:If guess not in word)。如果这样做,则Else语句中的对象将被重复,直到For循环停止。因此,最好在For循环之外使用separete If语句
word="banana"
display=[]
for i in word:
display+="_"
print(display)
while True:
Guess=input("Enter the letter:")
for position in range(len(word)):
if Guess==word[position]:
display[position]=word[position]
print(display)
if Guess not in word:
print("letter Doesn't exist")
下面的代码解决了此问题。您可以根据需要进行一些修改。如果单词中存在猜测的字母,则该字母将添加到显示变量中。如果不存在,则可以发出警告。但请注意,这可能会诱使您在for循环中编写ELSE语句(条件:If guess not in word)。如果这样做,则Else语句中的对象将被重复,直到For循环停止。因此,最好在For循环之外使用separete If语句
word="banana"
display=[]
for i in word:
display+="_"
print(display)
while True:
Guess=input("Enter the letter:")
for position in range(len(word)):
if Guess==word[position]:
display[position]=word[position]
print(display)
if Guess not in word:
print("letter Doesn't exist")
你不应该这样做:
list=[“刽子手”]
<代码>列表不应以这种方式重新定义。@shuttle87 Ok。。。感谢您提供的提示,但这是否是导致问题的原因?问题必须是您正在修改的隐藏单词
/到目前为止的单词
。一种故障排除技术是编写几行代码,只做您遇到问题的事情,然后分别处理,直到它正常工作为止。你学过函数了吗?@AkshayHazari我不是,但谢谢@是的,我已经学会了功能!为什么?你不应该这样做:list=[“刽子手”]
<代码>列表不应以这种方式重新定义。@shuttle87 Ok。。。感谢您提供的提示,但这是否是导致问题的原因?问题必须是您正在修改的隐藏单词
/到目前为止的单词
。一种故障排除技术是编写几行代码,只做您遇到问题的事情,然后分别处理,直到它正常工作为止。你学过函数了吗?@AkshayHazari我不是,但谢谢@是的,我已经学会了功能!为什么?我相信你所做的是对的!但这条线到底在干什么?隐藏的单词。替换(“”,“”)我相信你所做的是正确的!但这条线到底在干什么?隐藏的单词。替换(“\u”,“\ u”)