-3

this loop does not work as it does on another while loop I made that is almost identical to this one. I want it to loop until I input a valid answer. it asks me the first time and if I get it wrong it asks me one more time, but then it doesn't repeat after that. variable "a" never changes unless a valid input is put in. what did I do wrong? EDIT: first time using this site and I guess I wasn't specific enough, here is the full code with some fixed changes

selectcharacter = input("Select Your Character:")
while a == 1:
    if selectcharacter != 1 and selectcharacter != 2 and selectcharacter != 3:
        selectcharacter = input("Select Your Character:")
        continue
elif: selectcharacter == 1
    a == 2
    break
elif: selectcharacter == 2
    a == 2
    break
elif: selectcharacter == 3
    a == 2
    break
  • Are comparing a `string` to an `int`? – foxyblue Jan 09 '19 at 16:20
  • 2
    `a` never changes, period. Also, `input` returns a `str` value, not an `int`. – chepner Jan 09 '19 at 16:20
  • 3
    Possible duplicate of [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) – G. Anderson Jan 09 '19 at 16:22
  • the rest of the code is too long to put in, but under it says if a valid answer is put in, a == 2. that part works fine. i switched the input to raw inputs and it made it to where it doesn't ever ask again. – Zachary Stremmel Jan 09 '19 at 16:24

2 Answers2

1

I'm not sure why you are calling input twice in the body of the loop; let the loop condition do its job.

select_character = "0"
while select_character not in ["1", "2", "3"]:
    select_character = input("Select your character: ")

select_character = int(select_character)  # If you really want an int

If you don't want to explicitly initialize select_character, use an infinite loop with an explicit break, guarded by a condition you check after you call input.

while True:
    select_character = input("Select your character:" )
    if select_character in ["1", "2", "3"]:
        break
chepner
  • 497,756
  • 71
  • 530
  • 681
0

Not sure what the purpose of variable a is, and why you are comparing it to 1. I think you want a while True loop, and break out of it once a valid response is made. The user can provide a number between 1-3 (best case scenario), in which case it will successfully cast into an int below and reach the break line and exit the while loop. If the user provides a number outside of that range, the loop will continue to ask for a character selection. If the user mistakenly provides a non-integer value, it cannot be cast into an int, and when tried it will raise a ValueError for trying to convert a string into an int. The loop will then enter the except block and inform the user of the mistake and continue, meaning it will go back to the top of the loop.

while True:
    selectCharacter = input('Select your character:')
    # input() returns a str, must cast it to an int. 
    # Use try/except block to ensure the user provided an integer
    try:
        selectCharacter = int(selectCharacter)
    except ValueError:
        print('Please enter an integer only!')
        continue
    if selectCharacter >= 1 and selectCharacter < 4:
        print('Character selected')
        break
Endyd
  • 1,249
  • 6
  • 12