I am trying to create a small text-based game (no gui or anything, I am a novice with Python). The game consists of choice making, and I have this part where there are three choices, but one of the choices has another sub-choice in it. The loop that I am trying to create is that if the user's input is invalid, it prompts the user to type in their choice again.
Here is the code:
suba = ""
while suba == "":
answer2 = input("Do you understand? (Yes/No/Why?): ").lower()
if answer2 == "no":
subtext1 = """
"Looks like we've picked a dumb champion. Figure it out yourself."
"""
print(textwrap.fill(textwrap.dedent(subtext1),width=80))
print("""""")
if answer2 == "yes":
subtext2 = """
"Alright. I expect this to be done as soon as possible. Take into account that time in the other world is slower in our time. An hour in the new world is equivalent
to a day here. I do not expect this to be done overnight. Do not rush and die but do not take your time either."
"""
print(textwrap.fill(textwrap.dedent(subtext2),width=80))
print("""""")
if answer2 == "why":
print("""""")
subtext3 = """"Why what? Why do I ask of the Golden Heart or why do I ask for you not to kill the Wolf Queen?"
"""
print(textwrap.fill(textwrap.dedent(subtext3),width=80))
print("""""")
break
else:
suba = input("You have entered an invalid answer. Press enter to try again.")
print("""""")
suba = ""
while suba == "":
answer3 = input("What do you want to ask about? (Heart/Queen): ").lower()
if answer3 == "heart":
print("""""")
subtext4 = """"The Golden Heart supplies blood infused with golden nuggets. Whomever drinks the blood shall be granted immortality. Not only that, if we pour the blood onto
the soil, our crops will never die and there shall be resources to last eternity."
"""
print(textwrap.fill(textwrap.dedent(subtext4),width=80))
print("""""")
break
if answer3 == "queen":
print("""""")
subtext5 = """"The Wolf Queen was born from the Prince of Darkness. She is considered, what you call, a vessel. The Prince of Darkness has an infinite amount of dark magic that he
himself was not able to hold it all at once. He transferred his power into the first Champion to arrive at the new world. He ripped her soul from her shell, and used
the shell to store his dark magic. Since there was much, the dark magic manifested into it's own entity, now called the Wolf Queen. Killing the Wolf Queen will result
in killing only the shell, but the Prince of Darkness will be alarmed when an immense amount of dark magic is returned back into his body. He'll seek his vessel, and
see that the Golden Heart will be missing, and he will bring Hell. To prevent that, you must take the Golden Heart from her and not end her life."
"""
print(textwrap.fill(textwrap.dedent(subtext5),width=80))
print("""""")
break
else:
suba = input("You have entered an invalid answer. Press enter to try again.")
print("""""")
text5= "This is example text and not canon to the story"
When the user inputs an invalid input when prompt:
Do you understand? (Yes/No/Why?):
It immediately goes to:
You have entered an invalid answer. Please press enter to try again.
Why what? Why do I ask of the Golden Heart or why do I ask for you to not kill the Wolf Queen? (Heart/Queen):
When I want it to ask the first question again until the user enters yes, no, or why. Another thing is when the user inputs either "Heart" or "Queen" for the second question, it does it's thing, but immediately goes back to asking the first question when I'd like it to go to text5.
How can I fix this? I was playing around with the break/continue/pass loop functions but they don't do me any good. I apologize for the lengthy post, but I wanted to be as detailed as possible so I can get clear answers.
Aucun commentaire:
Enregistrer un commentaire