2

I have this function, which is part of a larger code I am working on for a class, so I am not looking for all the answers, just a little help with this part because I really don't know how to proceed. I have a function that asks for multiple inputs. firstname, lastname, bus row, and bus seat. these are then split and printed into a text file that I will do some other things with that i have not included the code for because I am still working on it. the first two entries need to be strings(first and last name), and the second two need to be numbers ( at least for now) the first number needs to be 1-4(row), and the second needs to be 1-15(seat), because that is all the seats available. I want an error to come back if anything else is entered in the following code.

def purchase():
    while True:
        try:
            firstname, lastname, bus_row, bus_seat = input(
                'Enter name first and last name and bus row and seat:').split()
            print("Name: {} {}\nrow {} seat {}".format(firstname, lastname, bus_row, bus_seat))
            with open('bookings.txt', 'a') as bookings:
                bookings.write('\n' + firstname + " " + lastname + " " + bus_row + " " + bus_seat)
                break
        except ValueError:
            print('Invalid entry')
    return

purchase()
dmitryro
  • 3,463
  • 2
  • 20
  • 28
hiker
  • 163
  • 1
  • 1
  • 8
  • Does this answer your question? [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) – Tomerikoo Oct 31 '20 at 22:29

3 Answers3

0

You will need to include something like this in your code:

try:
    bus_row = int(bus_row)
    bus_seat = int(bus_seat)
    if not 1 <= bus_row <= 4:
        raise ValueError("A bus row should be a whole number between 1 and 4")
    if not 1 <= bus_seat <= 15:
        raise ValueError("A bus seat should be a whole number between 1 and 15")
except ValueError as e:
    # handle the exception
Marko
  • 372
  • 2
  • 7
0

Don't assign firstname, lastname, etc. until you know they entered exactly four things. If they entered more or fewer, the assignment will fail.

# keep prompting until they enter correct input
while True:
    answers = input('Enter name and ...').split()
    if len(answers) != 4:
        print('You did not enter the correct number of answers')
        continue
    firstname, lastname, bus_row, bus_seat = answers
    if not firstname.isalpha():
        print('First name must be only letters')
        continue

    # do the same for last name ...

    if not bus_row.isdigit():
        print('Bus row must be a number')

    # do the same for bus seat ...

    # if we make it all the way here, the input must be correct, so break the loop
    break
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

This should work. You don't need to raise anything because you are already in a try/except. Simply you assert there are exactly 4 inputs and they are 2 strings and 2 integers. if they are not the program runs into an error and prints 'Invalid Entry'

def purchase():
    while True:
        try:
            variables = input('Enter name first name, last name, bus row and bus seat:').split()
            assert len(variables) == 4
            firstname, lastname, bus_row, bus_seat = variables
            for i in firstname:
                assert not i.isdigit()
            for i in lastname:
                assert not i.isdigit()
            bus_row = int(bus_row)
            bus_seat = int(bus_seat)
            assert 0 < bus_row < 5 and 0 < bus_seat < 16
            print("Name: {} {}\nrow {} seat {}".format(firstname, lastname, bus_row, bus_seat))
            with open('bookings.txt', 'a') as bookings:
                bookings.write('\n' + firstname + " " + lastname + " " + str(bus_row) + " " + str(bus_seat))
                break
        except:
            print('Invalid entry')

purchase()

Hope I helped

Cristiano
  • 267
  • 2
  • 10
  • That is great, it works perfectly. I was on to something very close to that, but not quite there. I was able to see what I was missing. thank you! – hiker Nov 01 '20 at 02:04