如果循环使用零件不是elif部分?

发布于 2025-02-10 04:27:12 字数 1582 浏览 2 评论 0原文

我正在尝试编码一个吊具游戏,其中它隐藏了一个单词的字母,一旦您猜到某些字母,它就会取代每个字母。

当我得到错误的答案时,我想将其放入一个列表中,我可以打印并向用户展示错误的字母数量(以便我可以做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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2025-02-17 04:27:12

问题源于以下事实:您正在让用户输入之后的猜测您已经检查了猜测>猜测是否在word中。您的循环应该看起来像

while correct_guess_amount < len(word):
    guess_letter = input("...")
    if guess_letter in word:
        # now you can safely call word.index(guess_letter)
    else:
        # handle the wrong answer case

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 the word. Your loop should look like

while correct_guess_amount < len(word):
    guess_letter = input("...")
    if guess_letter in word:
        # now you can safely call word.index(guess_letter)
    else:
        # handle the wrong answer case
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文