0
site = input("Choose a site:\n 1.site1 2.site2")
while site!=1 and site!=2:
    print("Wrong input please choose between 1 and 2")
    site=input("Choose a site:\n 1.site1 2.site2")

Here I'm trying to make the user choose between the number 1 and the number 2 but for some reason I cant make it work, specifically when I use the correct numbers as input it wont get out of the while loop so the program can run, I've also tried the following with no luck:

site = input("Choose a site:\n 1.site1 2.site2")
while True:
    if(site==1 or site==2):
        break
    else:
        print("Wrong input please choose between 1 and 2")
        site=input("Choose a site:\n 1.site1 2.site2")
martineau
  • 119,623
  • 25
  • 170
  • 301
ppcase
  • 23
  • 4
  • 3
    In Python 3.x `input` returns a string, so comparing it to an integer doesn't make sense and will always return `False`. – ForceBru Dec 20 '19 at 17:27
  • Try converting the input to int. – ctenar Dec 20 '19 at 17:29
  • Thank you! I've been trying to make it work for a while. – ppcase Dec 20 '19 at 17:29
  • @toolic There's no point trying to convert it to an `int` before you verify the input is valid; check against strings, then call `int` on a known valid choice. – chepner Dec 20 '19 at 17:49
  • @chepner You are right, but I prefer to not call int at all instead verify the input using strings instead of integers because it throws an error if the user uses a string as an input,since the valid answer is either 1 or 2 it's not a problem using strings insted of integers. – ppcase Dec 20 '19 at 18:00
  • @ppcase I'm not sure I follow. An input of `foo` should prompt the user for another input just as much as an input of `3`, rather than raising a `ValueError`. – chepner Dec 20 '19 at 18:04
  • @chepner i'm not refering to the solution of Ed Ward, I've tried running the following while giving an input of "foo"(note that i'm new to stackoverflow hence the link):https://controlc.com/cfc3481e – ppcase Dec 20 '19 at 18:14
  • 1
    Related: [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). – martineau Dec 20 '19 at 18:20

2 Answers2

1

For any input, you could try this function:

def enforced_input(prompt, options):
    if any([not type(i) == str for i in options]):
        raise ValueError("options should only contain string type")

    while True:
        result = input(prompt)
        if result in options:
            return result
        print("Please enter a valid input")

You can use it like this:

site = enforced_input("Choose a site:\n 1.site1 2.site2", ("1","2"))

Just remember that input returns a string, so make sure all your options are also strings, or it won't work.

Ed Ward
  • 2,333
  • 2
  • 10
  • 16
  • 1
    Making `options` optional (or at least, this default value) doesn't make much sense. I'd rather get a traceback about a missing argument than a function that never returns. – chepner Dec 20 '19 at 17:52
  • good point... have edited – Ed Ward Dec 20 '19 at 17:56
0

By defualt input() takes input as string to take input as int you can use int(input()) so your code will look like this

site = int(input("Choose a site:\n 1.site1 2.site2\n"))
while site!=1 and site!=2:
print("Wrong input please choose between 1 and 2")
site=input("Choose a site:\n 1.site1 2.site2\n")