0

I’m new to python trying to code an engineering problem which requires user input of predefined entries. For example I want input of sequence of defined names . say John, Mike, Smith, Adam. I need user to input from this list of names only. The issue I am facing is user can input any name not in defined list of names mentioned above. I wanted to limit choice of names to select rather than code an error message that name is not in the list.

Can this input requirement be coded ?

1
  • If the input should strictly be the above-mentioned names then, instead of taking input just give the user the option of selecting the names from the list only Commented Mar 27, 2021 at 16:09

5 Answers 5

1

Another way is to do it like this:

names = ['John', 'Mike', 'Smith', 'Adam']

name_num = 0
while name_num < 1 or name_num > len(names):
    message = ''.join([f'{i+1}: {n}\n' for i,n in enumerate(names)])
    name_num = int(input(message))
    
name = names[name_num-1]
print(name)

This also prints the available choises. The output looks like this:

1: John
2: Mike
3: Smith
4: Adam
_

If you want to handle bad inputs (no integers) you could also do something like this:

while name_num < 1 or res > len(names):
    message = ''.join([f'{i+1}: {n}\n' for i,n in enumerate(names)])
    try:        
        name_num = int(input(message))
    except:
        print('Input must be an integer value')
Sign up to request clarification or add additional context in comments.

Comments

0

This is the code you need:

name_list = ['John', 'Mike', 'Smith', 'Adam']

user_input = input('Please input a name: ')

if not user_input in name_list:
    print('Please input a valid name')

You can then adjust it depending on your case/needs.

Comments

0

Or this:

names = ["John", "Mike", "Smith, "Adam"]
print("1 : John")
print("2 : Mike")
print("3 : Smith")
print("4 : Adam")
number = int(input("input the number of the name you want"))
if number == 1:
  name = "John"

So on and so forth.

Comments

-1

try to take user input first and then loop the check if the user input is available in list or not . if not then raise exception. Show some of your code for more detail.

2 Comments

Looping would just waste CPU resources. You can use 'in' to check for a string inside a list
@MercifulSory inputs are by definition blocking the code execution until user input is given. I think any CPU optimization are killed by the reaction time of the user.
-1

You could use recursion. Although, there is a limit for recursion as well. So it depends on your use case. If you want to go for recursion, here is how

list_names = ['John', 'Mike', 'Smith', 'Adam']

def func():
    user_input = input('enter the name ')
    if user_input in list_names:
        print("valid user")
        #your code
    else:
        print("Invalid user")
        func()

Edit: The proposed limit for recursion appears to be increasing. Information on the limit and python version can be found at
https://www.python.org/dev/peps/pep-0611/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.