Search code examples
pythonvariableswhile-loop

Why is my While loop producing a repeat of the user’s input?


Edit: Thank you to everyone who commented, the solution was so simple. I had to delete the “print(understand)” at the bottom. To those asking why I didn’t do this or that, it’s because I have no idea what I’m doing, but I’ll get there.


In the while loop I have, an option is “yes” to lead to a variable’s output, but it’s also repeating the user’s “yes” input.

# Print list of input options
options = ("""
Here is a list of your options besides "yes" or "no":

  Help - Shows this list.

  Go Back - Head back to the previous room.
""")
help = ["help", "h", "options"]

# Go Back
go_back = ["go back", "back"]

# Yes
yes = ["yes", "y"]

# No
no = ["no", "n"]

Begin = lines = ["Story start"]


while True:
    understand = input("Do you understand?\n")
    if understand.lower() in help:
        print("\n")
        print(help)
        print("\n")
        continue
    elif understand.lower() in go_back:
            print("\nSorry, there's nowhere to go right now. Try again.\n")
        continue
    elif understand.lower() in yes:
        print("\n")
        print(Begin)
        break
    else:
        print(f"\nSorry, let's try this again.\n")
        continue

print(understand)

I apologize in advance, I’m very new to python and I don’t know how to format the question in this website.

I am asking the player if they understand previous instructions and when I test it using the “yes” variable, it outputs the “Begin” variable but for some reason it’s also repeating the user’s input “yes” afterwards. Like this:

Do you understand?
Yes

[‘Story start’]
Yes

I am unable to find why it’s reprinting “yes” after [‘Story start’].

Also, I am staying away from advanced methods right now as this is a small project I am doing for an online course

Thank you!


Solution

  • You typed 'print(understand)' in last string. Also move print in line 31) Maybe you wanted it to be a function?