Ask coding questions

← Back to all posts
Brainfart
jimxin2595 (18)

So I've been working on an improved version of ZombieHordeFight and I can't seem to figure out how to make it so that if you enter a number that isn't one of the options it'll give an error and make you give one that is....also any general suggestions/bugs you find would be greatly appreciated! Thanks!

Answered by SixBeeps (2907) [earned 5 cycles]
View Answer
Commentshotnewtop
OrangeJooce123 (67)
if CharacterChoice == "1":
  Character = "Mage"
elif CharacterChoice == "2":
  Character = "Warrior"
elif CharacterChoice == "3":
  Character = "Ranger"

I suggest you add an a else, as in:

if CharacterChoice == "1":
  Character = "Mage"
elif CharacterChoice == "2":
  Character = "Warrior"
elif CharacterChoice == "3":
  Character = "Ranger"
else:
print ('I don't understand.)

or something like that.

Please mark this as the answer if this helps!
IDK if this is spoonfeeding
If this is, just tell me.

jimxin2595 (18)

@OrangeJooce123 I mean like I don't want it to just print some text but give the player the option to try again

SixBeeps (2907)

@jimxin2595 You'll need a while loop to do that.

CharacterChoice = "bruh"
while (not CharacterChoice in ["1", "2", "3"]):
  CharacterChoice = input()
  # ...
PattanAhmed (1014)

@SixBeeps Hi,
I haven't stolen your code... Sorry for your misunderstanding.
Anyways, To maintain our friendship, I deleted that post.
Thanks for your confirmation...

GoodCoderBadBoy (2)
While True:
   CharacterChoice = input("")
   if CharacterChoice == "1":
      Character = "Mage"
      break                    # this exits the loop
   elif CharacterChoice == "2":
      Character = "Warrior"
      break                    # this exits the loop
   elif CharacterChoice == "3":
     Character = "Ranger"
     break                     # this exits the loop
   else:
      print("That is not an option, try again: ", end="")