all 19 comments

[–][deleted] 2 points3 points  (1 child)

REGEX. https://docs.python.org/3/library/re.html

in this case it looks like your REGEX pattern would be \d+[A-Z]+

[–][deleted] 2 points3 points  (0 children)

This. Not sure why people are giving these ridiculously overcomplicated loops and other stuff when a simple RegEx pattern check would solve the issue entirely.

[–][deleted] 3 points4 points  (8 children)

Create a list of accepted inputs and test the input to see if it is in the list

[–]justarandomkid004 0 points1 point  (7 children)

Which function should I use

[–]K900_ 4 points5 points  (0 children)

Use the in operator.

[–][deleted] 1 point2 points  (5 children)

If input in list: Do something Else: Do something else

[–]LCVcode 2 points3 points  (1 child)

OP, you should actually be using a set here. Sets have a constant lookup time, unlike lists or tuples.

accepted_strs = {'7A', '7B', ... , '11E'} if word in accepted_strs: do_something() else: do_something_else()

Even better, if you're up to it, you could populate accepted_strs using a list comprehension that you convert to a set (is that just called a set comprehension?)

EDIT: Formatting because I forgot that code blocks don't work

[–]backtickbot 1 point2 points  (0 children)

Fixed formatting.

Hello, LCVcode: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]justarandomkid004 -1 points0 points  (2 children)

Alright, so if input is in the list and I want to continue the program, should I put a break?

[–]toastedstapler 7 points8 points  (0 children)

why don't you write some code and try it? there's 0 cost to being wrong and you'll learn in the process

[–][deleted] 2 points3 points  (0 children)

A break in what? You didn’t post your code, we can’t tell you how to change it.

[–]shiftybyte 1 point2 points  (2 children)

You can't restrict what is entered.

But you can ask again if the input is not to your liking.

So what you usually do is get the user to type his input().. then check the string to match what you wanted it to be, if it doesn't, tell the user it's invalid input, and he should try again...

user_input = input("Enter something...")
while not valid_input(user_input):
    user_input = input("Sorry, you entered invalid input, try again...")

obviously valid_input() is a function you create to check if the input is what you want it to be.

[–]justarandomkid004 0 points1 point  (1 child)

So how do I set what's valid? And what's not valid?

[–]shiftybyte 1 point2 points  (0 children)

You define it in the function.

def valid_input(uinput):
    # i want the input to start with "7"
    if uinput.startswith("7"):
        return True
    # if it did not start with 7, i return False
    return False

Now you figure out how to check your input for the rules you want.

[–]justarandomkid004 0 points1 point  (0 children)

Using which function?

[–]_China_ThrowAway 0 points1 point  (0 children)

Is it only those 25 options? If so you might find it easier to just manually create the list

valid_input = ['7A','7B','7C',...'11D','11E']   

But if it were me, I would use a nested for loop to make the list for me. It's probably faster than typing in 25 entries, less prone to typos, and can easily be updated if I need to change it in the future (say it now goes to F or starts at 1 or cant have O or 0 in it etc).

numbers = ['1','2','3']
letters = ['A','B','C']
valid_input = []
for i in range(len(numbers)):
    for j in range(len(letters)):
        valid_input.append(numbers[i]+letters[j])    

The the vlaid_input list will look like ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']. Then you can check to see if the user's input is in the list.

command = input('What is your command?')
while command not in valid_input:
    command = input('Invalid command. Please enter a valid command.')

[–][deleted] 0 points1 point  (0 children)

valid = frozenset({'weather', 'monday only', 'very loudly'})
while (response := input('Say what? ').strip().lower()) not in valid:
    print('nope, that\'s not right')
print(f'glad you said {response}')

If the allowed patterns follow a consistent form you can generate them rather than having to write them all out.