0

Hi I have an assignment due about a code on superheroes and i'm new to coding we haven't even started GCSE level yet...

Heroes = ["Batman","Wonder Woman","Superman","Spiderman"]
print(Heroes)
print("Current pilot: ",Heroes[0])
print("Co-pilot:",Heroes[1])
print ( Heroes[2],"has been temporarily replaced by Hit Girl")
Heroes[2]="Hit Girl"
print("Current heroes are now:")
print(Heroes)
print("There are two new superheroes")
print("Current heroes are now:")
Heroes.append("The Scarlet Witch")
Heroes.append("Valkryie")
print(Heroes)
Answer = None
while Answer not in ("Yes", "No"):
    Answer = input("Would you like to change a Hero? ")
    if Answer==("Yes"):
       Number=int(input("Choose a Hero from 0-5"))
       print("You will replace Hero:",Number)
       Name=str(input("Enter Hero name:"))
       print("New Hero name is:",Name)
       Heroes[Number]=Name
       print(Heroes)
    elif Answer==("No"):
        print("Ok this is the final list of Heroes")
        print(Heroes)
    else:
        print("That isn't a Yes or No answer...")


I would really like to know on how to go back to the "Yes" section when I have finished that section and to still have a "Yes" and "No" to end the script.

Sorry if it is a lot to ask help on...

aesthii
  • 27
  • 3
  • 2
    You need a second loop. Python doesn't have a `goto` statement to transfer control to an arbitrary line. – chepner Feb 29 '20 at 17:41
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Jongware Feb 29 '20 at 17:59

2 Answers2

1

I believe that you are looking for the termination of the while loop only when the answer is "No". The simplest way to do that would be to remove the "Yes" from the line containing while loop. while Answer not in ("Yes", "No"): becomes: while Answer != "No":

Hope that helps.

  • 2
    Or, more simply, `while Answer != "No":`. – Matthew Strawbridge Feb 29 '20 at 17:56
  • 1
    @AmitTrivedi: `while Answer not in ("No"):` is wrong because it's equivalent to `while Answer not in ("N", "o"):`. You need an extra comma to make that a tuple, like `("No",)`. – Guimoute Feb 29 '20 at 18:04
  • @MatthewStrawbridge and #Guimoute thanks for the correction. Being new to python, I had mistakenly taken the tuple to be a list. Edited the original answer. Thanks again. – Amit Trivedi Mar 01 '20 at 07:00
0

You want the user to play until the user says no to the game? For that the code will be something like this.

Heroes = ["Batman","Wonder Woman","Superman","Spiderman"]
print(Heroes)
print("Current pilot: ",Heroes[0])
print("Co-pilot:",Heroes[1])
print ( Heroes[2],"has been temporarily replaced by Hit Girl")
Heroes[2]="Hit Girl"
print("Current heroes are now:")
print(Heroes)
print("There are two new superheroes")
print("Current heroes are now:")
Heroes.append("The Scarlet Witch")
Heroes.append("Valkryie")
print(Heroes)

while True:
    Answer = input("Would you like to change a Hero? ")
    if Answer==("Yes"):
       Number=int(input("Choose a Hero from 0-5"))
       print("You will replace Hero:",Number)
       Name=str(input("Enter Hero name:"))
       print("New Hero name is:",Name)
       Heroes[Number]=Name
       print(Heroes)
    elif Answer==("No"):
        print("Ok this is the final list of Heroes")
        print(Heroes)
        break
    else:
        print("That isn't a Yes or No answer...")
krishna
  • 1,029
  • 6
  • 12