-1

So, I am trying to code a text-based adventure game here. I am doing this in order to master branches, functions, and, importantly, loops (while and for). My game is pretty big, so here is the exact section of the code I am stuck with:

def grand_battle():
    print("You head up north.")
    print("Suddenly, 4 spaceships emerge. They all emerged from behind astroids in the area. The area is filled with astroids and is tough to navigate.")
    print("You must activate your defense systems, but your ships hits an astroid.")
    print("You have a couple of options here.")
    print("First, you can focus firepower on the 4 unidentified ships.")
    print("Second, you can try to escape by firing suppressing fire at the other 4 ships. This ensures they keep a distance while your crew repair the ship.")
    print("Third, you can chase the cargo ship. You and your crew can embark it, kill or capture all crew memebers inside, and use this new ship to escape.")
    print("Fourth, you tell the spaceships you are freindly. You inform them that all you need is passage through this area, to arrive to your intended destination (this is a lie, as you know).")
    print("Which option do you pick")

    option = input("> ")

    if "First" in option:
            print("Your automatic turrets pierce through 2 adjacent ships. The main dock is broken and their crews are in distress. The ships loose balance, fire begins to engulf them, and a huge amount of metal scrap starts to float around.")
            print("You have destroyed the first two ships.")
            print("Meanwhile, the two other ships decide to surround you.")
            print("One heads directly towards you. It fires two missiles at your ship.")
            print("Your turrets hit the ship, but your ship is also hit by 1 of the missiles; the other hit an astroid.")
            print("Your ship is now in distress and emergency sounds are triggered inside the cabin.")
            print("The other ship behind you crashes on an astroid. Lucky.")
            print("The cargo ship escapes.")
            print("You and your crew mates take the escape pods and you all land in some rimworld.")
            print("What happens there is a story for another time.")
            print("Game over.")

      if "Second" in option:
            print("The ships keep a distance and try to blast you from a distance.")
            print("You are overwhelmed by the hostile firepower.")
            print("You have two options: ")
            print("One, you take the escape pods and hope you land somewhere safe.")
            print("Two, you keep firing. If you are lucky, the ship is repaired and you can escape the place.")
            print("Which option do you choose")

            option1 = input("> ")

            if "One" in option1:
                print("Your crew escapes. So do you.")
                print("You land in a rimworld far far away. What happens there is a story for another time.")
                print("Game over.")
            elif "two" in option1:
                print("The crew repairs the ship. Your trust and patience have been rewarded.")
                print("Congratulation, the ship works better than ever.")
                print("You turn back and launch hyperdrive.")
                print("You survived.")
                print("Good job!")
                print("Game over.")
            else:
                dead()

     if "Third" in option:
            print("You enter the ship. It is dark. There are two doors. One to your left and another to your right.")
            print("Which door do you choose?")

            door = input("> ")

            if door == "left":
                print("You find a snake inside.")
                dead()
            elif door == "right":
                gold_room()
            else:
                print("You did not react.")
                print("You ran out of time and the ship has been boarded by ennemy forces.")
                dead()
     if "Fourth" in option:
            print("The spaceships escort you away from the cargo ship.")
            print("Game over")

     else:
            print("Learn to type!")
            dead()

So, this is a function that is triggered by another choice the user makes earlier in the game. In this function, I ask the user to choose from First to Fourth. How can I write a condition where if the user types anything other than "First" "Second" "Third" "Fourth", the program replies back to the user saying "Oops! I did not understand that" "Try again". After the try again, the user will be given the option prompt once more. If he puts the wrong answer, he will receive the same message. If he puts the right answer, the program continues.

Mega
  • 11
  • 4
  • no, because that one has integers. I want an example with strings. Also, I already saw that one before and tried out what it says. – Mega May 26 '20 at 11:39
  • 1
    Maybe the question is with integers. The answer is general and even covers strings and actually has your exact use-case under the heading *Implementing Your Own Validation Rules*. In general read about [mre] and [ask]. If you saw this link and it didn't help you, you should write how it didn't help and what are the exact problems you are currently facing. It is not necessary to put your whole program here, just the shortest code that recreates your problem/error – Tomerikoo May 26 '20 at 11:53
  • Thank you for taking the time to help. If you mind, I would like just a small elaboration on that section. So, under that heading, the person writes a code that verifies whether all characters are in caps. he puts: "if not data.isupper():". How do I rewrite that specific part of the code, so that it works with my program. Do I put: if not "First" or "Second" or "Third" or "Fourth": ? – Mega May 26 '20 at 12:21
  • Look at the second loop in that code section. Something like `if data.lower() not in ('first', 'second', 'third', 'fourth'):` is what you need. In your case you can actually just wrap all the `if`s with the `input` inside a `while True` loop and make sure you `break` when needed – Tomerikoo May 26 '20 at 12:51
  • Loops are very basic for any programming language and are used a lot. I'd suggest Googling it. – HazardousGlitch May 26 '20 at 13:02

1 Answers1

0
while isAlive:
   //do something

and use any condition you want and you can use if else if in your code and also you can check if the input is not equal to any of them i.e.

   if option != "first" and  option != "second" (..so on):
    print ('Enter a valid input');
  • Hi. Thank you for your response. Can you explain the first part of your suggestion? – Mega May 26 '20 at 11:41
  • in your function make a while loop and when the set a variable to true initially and then when the character dies make it false so that the loop will break. and make an if condition to check that if the input matches and option if another input is entered rather than expected then print enter a valid input – Veer Metri May 29 '20 at 08:24