Untitled

 avatar
unknown
python
2 years ago
3.0 kB
16
Indexable
plates = []  # File-wide list representing the stack of plates


# Function that adds plates to the stack. Will perform sanity checks and inform the user if the request is invalid.
def add_plate():
    print('Add a plate')
    print('=' * 11)
    print('Enter a plate size: ')

    # Pause for user input and store the value as 'plate_size'
    plate_size = int(input())

    # Conditional logic to perform sanity checks and add plates if request is sane
    if plate_size < 1:  # Not a sane value
        print(f'Can\'t place a plate of size {plate_size} on the stack. Please choose a value greater than zero')
    elif len(plates) > 0 and plate_size > plates[-1]:  # Plate size violates rule of top-down ascending order
        print(f'Can\'t place a plate of size {plate_size} on top of another plate of size {plates[0]}.')
    else:  # Sane request, adding to stack
        plates.append(plate_size)
        print('Success!')


# Function that prints the number of plates on the stack
def print_plate():
    print('Print plates')
    print('=' * 11)

    if not plates or plates[0] == 0:
        print('There are no stacked plates.')

    for plate in reversed(plates):
        print(plate)


# Function that removes plates from the stack. Will perform sanity checks and inform the user if the request is invalid.
def remove_plate():
    if (len(plates)) < 1:  # No plates to be removed
        print('No plates to remove.')
        return

    print('Remove plates')
    print('=' * 11)
    print(f'{plates[-1]} plates on top')
    print('How many plates to remove?: ')

    # Pause for user input and store the value as 'plate_size'
    plate_size = int(input())

    # Conditional logic to perform sanity checks and add plates if request is sane
    if plate_size > plates[-1]:  # Plate size violates rule of top-down ascending order
        print(f'Can\'t remove more than {plates[-1]} plates. You chose {plate_size}.')
    elif plate_size < 1:  # Plate size is nonsensical
        print('Can\'t remove less than 1 plate.')
    else:  # Sane request
        if plates[-1] - plate_size == 0:
            plates.pop()  # Top plate has no elements, we remove it
        else:
            plates[-1] = plates[-1] - plate_size  # Plates are removed from top of stack
        print('Success!')


# Runner function
def run():
    while True:  # Create Menu
        print('Menu')
        print('=' * 16)
        print('0. [Exit]')
        print('1. Add a Plate')
        print('2. Print Plates')
        print('3. Remove Plates')
        print('Select [0-3]:')

        # Pause for user input and store the value as 'selection'
        selection = input()

        match selection:
            case '0':
                print('Goodbye!')
                break  # Program exit
            case '1':
                add_plate()
            case '2':
                print_plate()
            case '3':
                remove_plate()
            case _:
                print('Invalid')


if __name__ == '__main__':
    run()
Editor is loading...
Leave a Comment