Python输入验证程序语法疑问及演出售票统计功能实现咨询
Hey there! I get it—sometimes Python books skip over these practical, real-world input validation scenarios. Let’s build a solution for your theater group’s ticket tracking that’s robust and easy to follow.
Step-by-Step Solution
First, let’s outline what we need to do clearly:
- Ask the user for ticket counts for 4 separate shows
- Make sure each input is a valid number between 0 and 120 (inclusive)
- If someone enters something invalid (like a letter, or a number over 120), show an error and ask again
- Finally, calculate the total tickets sold and the average attendance per show
Here’s a complete, working Python script that does all this:
total_tickets = 0 num_shows = 4 for show in range(1, num_shows + 1): while True: try: # Get input from user and convert to integer tickets = int(input(f"Enter number of tickets sold for show {show}: ")) # Check if the number is within the valid range if 0 <= tickets <= 120: total_tickets += tickets break # Exit the loop since we have valid input else: print("Error: Ticket count must be between 0 and 120. Please try again.") except ValueError: # Handle cases where user enters non-numeric text print("Error: Please enter a valid whole number.") # Calculate average attendance average_attendance = total_tickets / num_shows # Display final results print("\n--- Ticket Sales Summary ---") print(f"Total tickets sold: {total_tickets}") print(f"Average attendance per show: {average_attendance:.1f}")
How This Works
Let’s break down the key parts so you understand every step:
- Outer Loop for Shows: The
for show in range(1, 5)loop runs 4 times (once for each show), so we collect input for each performance. - Inner Validation Loop: The
while Trueloop keeps running until we get a valid input. This is how we force the user to try again if they mess up. - Handling Non-Numbers: The
try-exceptblock catchesValueError—this happens when someone enters something like "abc" instead of a number. We print an error message and let the loop restart. - Range Check: After confirming the input is a number, we check if it’s between 0 and 120. If not, we show an error and loop back to ask again.
- Calculations: We add each valid ticket count to
total_tickets, then divide by 4 to get the average. The:.1fformats the average to one decimal place for readability.
Example Run
Here’s what it looks like when someone uses the program (including invalid inputs):
Enter number of tickets sold for show 1: 150 Error: Ticket count must be between 0 and 120. Please try again. Enter number of tickets sold for show 1: hundred Error: Please enter a valid whole number. Enter number of tickets sold for show 1: 100 Enter number of tickets sold for show 2: 95 Enter number of tickets sold for show 3: 0 Enter number of tickets sold for show 4: 120 --- Ticket Sales Summary --- Total tickets sold: 315 Average attendance per show: 78.8
This script is flexible too—if you ever need to track more shows, just change the num_shows variable to the new number!
内容的提问来源于stack exchange,提问作者jercai
相关产品推荐
相关产品推荐

