Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Matching either Regex pattern or list contents in Python

Hey everyone!

I'm sure the answer to this is pretty simple, but I'm having trouble figuring out how to accomplish this task:

I am creating a program in which the user needs to input a ticket number (the ticket number is a digit that is 8 or more characters) . I also want the program to have options like help, show, quit, etc. I know that I can ensure the user is inputting a valid ticket number using a regex pattern:

answer = input('enter ticket number: ')

if re.match(r'\d{8,}', answer): 
    print('answer is valid') 
else: 
    print('answer is invalid')

The problem I'm having is that I can't figure out how to make the input match either the regex pattern OR one of the options and return an 'invalid input' message if it doesn't match either. Any thoughts?

Hope this makes sense! Again, any help would be greatly appreciated!

3 Answers

Are you trying not to use an or?

Is this what you are trying to achieve?

import re

answer = input('enter ticket number: ')

if (
    re.match(r'\d{8,}', answer) or 
    answer.lower() == 'quit' or 
    answer.lower() == 'help'
    ): 
    print('answer is valid') 
else: 
    print('answer is invalid')

Thanks! That does basically work for what I'm trying to accomplish. However, I'm having a new problem moving forward from there, ultimately I want a loop that makes the user input either the ticket number or one of the options, and then continues from there (ultimately it's going to append the ticket number to a list and do stuff with it). Here's a shortened version of what I have so far:

import re
import datetime

ticketlist = []

while True:
    time = datetime.datetime.now().strftime("%I:%M %p")
    print('enter ticket number or help for options: ')
    answer = input('Ticket ID: ')
    if answer.lower() == 'help':
        print('\n "quit" to quit \n "show" to show tickets  \n "num" for number of tickets \n "clear" to clear screen')
    elif answer.lower() == 'quit':
        break
       ##do  stuff if user inputs the other options
    else:
        ticketlist.append(answer + " " + time)
        print('{} was logged at {}'.format(answer,time))

Where I'm stuck now is trying to continue with the other part of the program and still keep the 'input ticket' part within its own while loop. Does that make sense? Thanks again!

Yep - that's the fun of programming.

I would recommend you layout the flow first and then develop the code.

Maybe some pseudo code or a flow chart to start and show where the key functions need to be and the business rules around them.

Continuous testing and rework is the fun part - along with looking up everything in the documentation - glad you're moving forward :smile:

Thanks for the help! I think I finally got it! I realized that I could take a different tack and instead of trying to make the input match I could EXCLUDE the input with 'not' and it would work:

options = ['help', 'show', 'quit']

while True:
    answer = raw_input('enter ticket number or help for options: ')
    if not(
        re.match(r'\d{8,}', answer) or answer in options
        ):
        print('invalid response')
    elif answer == 'quit':
        break   

etc.

Thanks again! sometimes it helps to just talk it through :)