0

I am allowing choices to user and if the user's choice isn't a number then the script should show an error message: "Please try again".

def prompt(message, choices):
  while True:
    choice = input(message)
    if choice in choices:
      return choice
    print("That was not a valid answer. please retry\n"
          "valid answers are", choices)

def aORp():
  choices = list(range(1, 17))
  length= prompt("What is your length? ",choices)
0xc0de
  • 8,028
  • 5
  • 49
  • 75
Arcanium
  • 41
  • 4
  • 2
    Please, can you clarify what exactly are you asking? – klappvisor Jun 17 '16 at 08:44
  • Please fix the indentation. EDIT : Ty @0xc0de – Nab Ilovich Jun 17 '16 at 08:47
  • I didn't say what was wrong. I want the user to be able to enter any number but if they don't then it runs the error message. All I need is to be able to word the choices =alternative to "list(range(1,17))" so choices = any number but not a letter or word and if so it runs the error message. – Arcanium Jun 17 '16 at 08:47
  • 2
    +1 @klappvisor. To questionner: it's good that you have tried something and shown us, but it's unclear what's your question or problem. Please phrase it like a question (I have fixed your code and saw a bug but I'm not sure if that't your question, so I've let the bug stay in). – 0xc0de Jun 17 '16 at 08:49
  • I need an alternative to choices = list(range(1,17)) because any number triggers the error message "That was not a valid answer....." Obviously the choice of list and range are wrong for what I want to do - which is have the user choose any number but not a string. – Arcanium Jun 17 '16 at 08:52
  • 1
    It's because choice is a string while choices is a list of int, so `choice in choices` is always False. You need to convert choice to int. – polku Jun 17 '16 at 08:53
  • This is all the code: – Arcanium Jun 17 '16 at 08:54
  • So you are saying change to int in choices? – Arcanium Jun 17 '16 at 08:55
  • @user2527918 exactly ...if int(choice) in choices:..... But wrap that in a try/except block – Ma0 Jun 17 '16 at 08:56

3 Answers3

0
import sys     
def prompt(message, choices):
    while True:
        choice = input(message)
    if choice in choices and type(choice) == int:
        return choice
    else:
        sys.exit("That was not a valid answer. please retry\n"
                 "valid answers are: {}".format(choices))

def aORp():
  choices = list(range(1, 17))
  length= prompt("What is your length? ",choices)

by adding the type(choice) == int you prevent anything apart from integers to be returned, anything else will result into the termination of the program.

Edit: Maybe you don't your program to terminate on faulty input, but that's up to you!

Edit2: As the comments stated, this doesn't work, actually. Since an answer was given, I'll leave it.

Ian
  • 1,688
  • 18
  • 35
  • Im not sure but `type(choice) == int` won't match other numeric types than int. But the questionner asked for all numeric types. – Nab Ilovich Jun 17 '16 at 09:02
  • this does not work. choice is string since it is input by the user so the if statement is always False – Ma0 Jun 17 '16 at 09:12
  • Whoops you are both correct. An answer was already given, I'll write an edit. – Ian Jun 17 '16 at 09:16
0

What you need is to use float

float(choice)

It'll return True if choice is a number. It'll return an error if choice is not a number.

More info there :

How do I check if a string is a number (float) in Python?

EDIT : As mentionned in the comments my answer isn't good, cause choice is compared to a list of integers then float is not the best choice.

Community
  • 1
  • 1
Nab Ilovich
  • 360
  • 5
  • 14
  • i would go with int(choice) since the list he is comparing against contains items of type integer. – Ma0 Jun 17 '16 at 08:55
  • The questionner said that `choice` has to be a number, that includes `int` `float` and other numeric types. Or am I missing something ? – Nab Ilovich Jun 17 '16 at 09:00
  • 1
    he is checking the number input by the user against a list created using range() so converting the input to float might mess up the comparison performed with "if choice in choices:" – Ma0 Jun 17 '16 at 09:06
  • 1
    The issue is the line choices = list(range(1,17)) - this doesn't let the user pick a number - instead it says the answer should be [1,2,3,4.....16] – Arcanium Jun 17 '16 at 09:06
0

You can write a function which inputs a number and then converts it to an int, but doesn't work if it is anything else. If I understand you right, you only want the user to be able to enter an int but not a string. If you only want to check that, an example is:

def prompt (message):
    try:
        choice = int(input(message))
        return choice
    except ValueError:
        print("That is not a valid answer, please try again.")
        return prompt(message)

That's generally how I do it, but you can definitely use a while loop if you want.

Edit: This allows you to define the choices you want:

def prompt (message, choices):
    try:
        choice = int(input(message))
        if choice in choices:
            return choice
        else:
            print("Not a valid input, please try again. \n",
            "Valid inputs are:", (choices))
            return prompt(message, choices)
    except ValueError:
            print("Not a valid input, please try again. \n",
            "Valid inputs are:", choices)
            return prompt(message, choices)
masteryoom
  • 81
  • 6
  • The issue is the line choices = list(range(1,17)) - this doesn't let the user pick a number - instead it says the answer should be [1,2,3,4.....16 – Arcanium Jun 17 '16 at 09:09
  • @user2527918 You mean you want every number (int or float) between 1 and 17 to be considered valid ? – polku Jun 17 '16 at 09:15
  • I have added new code in an edit to allow for defining any choices you want. – masteryoom Jun 17 '16 at 09:16