def get_main_menu_choice(): ''' Get a valid main menu choice from the user. Returns ------- choice : string The choice. ''' choice = '' while not is_menu_choice_valid(choice): choice = input_menu_choice() if not is_menu_choice_valid(choice): print('Plese enter a valid command.') return choice def input_menu_choice(): ''' Show the main menu, and ask the user for a choice. Might not be valid. Returns ------- choice : TYPE DESCRIPTION. ''' print() print('Menu') print('----') print(' S: sale') print(' R: return') print(' T: sTatus report') print(' Q: quit') choice = input('Command? ') choice = choice.strip().lower() return choice def is_menu_choice_valid(choice): ''' Check whether a string is a valid main menu command. Parameters ---------- choice : string Command to check. Returns ------- valid_choice : boolean True if valid. ''' valid_choice = \ choice == 's' \ or choice == 'r' \ or choice == 't' \ or choice == 'q' return valid_choice def ticket_sale(): ''' Ask the user how many tickets were sold. Returns ------- tickets : int User response. ''' tickets = get_tickets_amount('How many sold? ') return tickets def ticket_return(): ''' Ask the user how many tickets were returned. Returns ------- tickets : int User response. ''' tickets = get_tickets_amount('How many returned? ') return tickets def get_tickets_amount(prompt): ''' Ask the user for a ticket amount. Parameters ---------- prompt : string Question to ask. Returns ------- amount : int Number of tickets. ''' is_input_ok = False while not is_input_ok: # Start off assuming data is OK. is_input_ok = True # Get input from the user. user_input = input(prompt) # Is the input an integer? try: amount = int(user_input) except ValueError: print('Sorry, you must enter a number.') is_input_ok = False # Check the range. if is_input_ok: if amount < 1 or amount > 10: print('Sorry, please enter a number from 1 to 10.') is_input_ok = False return amount def show_status_report(inventory, sold): ''' Show a sales status report. Parameters ---------- inventory : int Inventory. sold : int Sales. Returns ------- None. ''' print() print('Status report') print('------ ------') print('Ticket inventory:', inventory) print('Tickets sold:', sold) # --------------- Main program ------------- # Initialize inventory and sales. current_inv = 50 total_sold = 0 # Loop until the user quits. command = '' while command != 'q': command = get_main_menu_choice() if command == 's': # Sale. num_sold = get_tickets_amount('How many sold? ') # Check whether we have the inventory. if num_sold > current_inv: print("Sorry, we don't have that many tickets.") else: # Adjust inventory and sales. current_inv -= num_sold total_sold += num_sold print('Cha-ching!') elif command == 'r': # Return. num_returned = get_tickets_amount('How many returned? ') # Have we sold that many? if num_returned > total_sold: print("Sorry, we haven't sold that many tickets.") else: # Adjust inventory and sales. current_inv += num_returned total_sold -= num_returned elif command == 't': # Show the status report. show_status_report(current_inv, total_sold) print() print('OK, bye!')