# Functions go here def int_check(question): """Checks users enter an integer""" error = "Oops - please enter an integer" while True: try: response = int(input(question)) return response except ValueError: print(error) def not_blank(question): """Checks that a user response is not blank""" while True: response = input(question) if response.strip() != "": return response print("Sorry, this can't be blank. Please try again.\n") def string_check(question, valid_ans_list=('yes', 'no'), num_letters=1): """Checks that users enter the full word or the first letter of a word from a list of valid responses""" while True: response = input(question).lower() for item in valid_ans_list: if response == item or response == item[:num_letters]: return item print(f"Please choose an option from {valid_ans_list}") # Main routine goes here # Initialise valid options payment_ans = ('cash', 'credit') MAX_TICKETS = 5 tickets_sold = 0 # Keep going until 5 valid tickets are sold while tickets_sold < MAX_TICKETS: print(f"\nTicket {tickets_sold + 1} of {MAX_TICKETS}") name = not_blank("Name: ") age = int_check("Age: ") if age < 12: print(f"{name} is too young") continue elif age > 120: print(f"{name} is too old") continue pay_method = string_check("Payment method (cash/credit): ", payment_ans, 2) print(f"{name} has bought a ticket ({pay_method})") tickets_sold += 1 # Only count valid tickets # After the loop finishes print("\nšŸŽ‰ You sold all the tickets!")