如果循环使用零件不是elif部分?
我正在尝试编码一个吊具游戏,其中它隐藏了一个单词的字母,一旦您猜到某些字母,它就会取代每个字母。
当我得到错误的答案时,我想将其放入一个列表中,我可以打印并向用户展示错误的字母数量(以便我可以做hangman绘图)。
我可以让它进入列表一次,然后一旦我做了正确的字母,它就不起作用了,我必须再次重新重新重新重新注释正确的字母。然后,我去错了一封错误的信,它不再起作用,我会遇到错误:
valueerror:找不到子字符串 - 位置=(word.index(uphus_letter))
我认为它正在使用上述内容,并且我不确定如何修复它。这就是循环的样子:
if guess_letter in list_of_letters:
position = (word.index(guess_letter))
print(hiding.replace(hiding[position], guess_letter))
hiding = hiding.replace(hiding[position], guess_letter)
correct_guess_amount += 1
while correct_guess_amount <len(word):
if guess_letter in list_of_letters:
guess_letter = input('enter a letter: ')
position = (word.index(guess_letter))
print(hiding.replace(hiding[position], guess_letter))
hiding = hiding.replace(hiding[position], guess_letter)
correct_guess_amount += 1
print(correct_guess_amount)
elif guess_letter not in list_of_letters:
incorrect_guess_amount += 1
incorrect_list.append(guess_letter)
print(incorrect_list)
print(incorrect_guess_amount)
guess_letter = input('enter a letter: ')
if guess_letter in list_of_letters:
guess_letter = input('enter a letter: ')
position = (word.index(guess_letter))
print(hiding.replace(hiding[position], guess_letter))
hiding = hiding.replace(hiding[position], guess_letter)
correct_guess_amount += 1
print(correct_guess_amount)
I am trying to code a hang man game where it hides the letters of a word and it replaces each letter once you guess that certain letter.
When I get a wrong answer, I want to put it into a list which I can print and show the user the count of wrong letters (so I can do hangman drawing).
I can get it to go into a list once and then once I do a correct letter it does not work the first time and I must reinput the correct letter again. I then go to a wrong letter, it no longer works and I get an error:
ValueError: substring not found - position = (word.index(guess_letter))
I think it is using the above if and I'm unsure how to fix it. This is what the while
loop looks like:
if guess_letter in list_of_letters:
position = (word.index(guess_letter))
print(hiding.replace(hiding[position], guess_letter))
hiding = hiding.replace(hiding[position], guess_letter)
correct_guess_amount += 1
while correct_guess_amount <len(word):
if guess_letter in list_of_letters:
guess_letter = input('enter a letter: ')
position = (word.index(guess_letter))
print(hiding.replace(hiding[position], guess_letter))
hiding = hiding.replace(hiding[position], guess_letter)
correct_guess_amount += 1
print(correct_guess_amount)
elif guess_letter not in list_of_letters:
incorrect_guess_amount += 1
incorrect_list.append(guess_letter)
print(incorrect_list)
print(incorrect_guess_amount)
guess_letter = input('enter a letter: ')
if guess_letter in list_of_letters:
guess_letter = input('enter a letter: ')
position = (word.index(guess_letter))
print(hiding.replace(hiding[position], guess_letter))
hiding = hiding.replace(hiding[position], guess_letter)
correct_guess_amount += 1
print(correct_guess_amount)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题源于以下事实:您正在让用户输入之后的猜测您已经检查了
猜测>猜测
是否在word
中。您的循环应该看起来像The problem arises from the fact that you are having the user input the guess after you have checked if the
guess_letter
is in theword
. Your loop should look like