Marstonliu 发表于 2022-9-11 17:03:19

订座系统

各位高手,有个电影院订座的作业,请您帮手:
1. 60座位影院。。
2. 向客人展示座位,并请客户选择位置
3. 可同时选择多个座位
4. 如座位不可用,给客户信息并请他重选
5. 最好可以用上map 和 try-except-else
6. 最后告知售票的数量、上座率和已经赚取的收入。
7.有方法预防客人选择不可用的座位。

Marstonliu 发表于 2022-9-11 22:52:26

请高手帮忙检查一下,并加上最后的6、7项次。感谢!
# Ticket Booking System.
def print_tickets():
    """Print the tickets of the user."""
    for user_name, seats in user_tickets.items():
      print(f"\nYou, {user_name.title()}, have chosen {len(seats)} seat(s).")
      for seat in seats:
            print(f"\tSeat number: {seat}")


# Empty dictionary to store info later on.
user_tickets = {}

# List of seats the user can choose from.
available_seats = ['1a', '2a', '10b', '10d', '10e', '10g', '10f', '8f', '8a', '9g' ]


# All prompts.
start_prompt = "\nWould you like to start booking your ticket? (yes/no) "
wanted_seats_prompt = "\nHow many seats are you booking today?"
wanted_seats_prompt += "\nEnter the number: "
name_prompt = "What is your name? "
seat_prompt = "\nPlease enter the number of the seat you would like to book: "
go_again_prompt = "Would you like to let someone else book their tickets? (yes/no) "


print("Welcome To The Seat Booking Portal!")

# Ask the user if he would like to start booking their tickets.
start = input(start_prompt)
if start.lower() == 'yes':
    # Runs until it reaches a break statement.
    while True:
      # Empty list to store the seat(s) the user has chosen.
      seats = []

      # Find out how many times to run the while loop.
      wanted_seats = input(wanted_seats_prompt)
      # Convert the string representation of the number to an integer representation.
      wanted_seats = int(wanted_seats)
      # If the user has asked for more seats than the number of seats
      # available execute this block.
      if wanted_seats > len(available_seats):
            print(f"\n--I'm sorry, we only have {len(available_seats)} "
                "seats available--")
            print("--Please try again--")
            continue

      # Ask for the users name.
      user_name = input(name_prompt)

      # Run until the user has chosen the requested number of seats.
      while True:

            # Show the user the available seats.
            print("\nHere are the available seats:")
            for seat in available_seats:
                print(seat)

            # Ask the user for their chosen seat number.
            seat = input(seat_prompt)

            # If the user has entered a seat that is in the 'available_seats'
            # list; remove it from the 'available_seats' list.
            if seat in available_seats:
                available_seats.remove(seat)
            # The user has entered a seat that is not in the 'avialbe_seats' list.
            # Ask for their seat again.
            else:
                print("\n--I'm sorry you have chosen an invalid seat--"
                  "\n-Please try again-")
                continue

            # Add the chosen seat to the 'seats' list
            seats.append(seat)

            # If the user has said that they are going to book more than one seat
            # go through this seat booking 'while' loop again.
            if wanted_seats > 1:
                print("\nYou can now choose another seat.")
                # The loop will run a limited number of times.
                # It will only 'continue' when there is more than one 'wanted_seat'.
                wanted_seats-=1
                continue
            else:
                break

      # Add the 'user_name' variable and 'seats' list to the 'user_tickets' dictionary.
      user_tickets = seats

      #If their are any available seats left ask the user if he
      # wants to let another person book their tickets.
      if available_seats:
            go_again = input(go_again_prompt)
            if go_again == 'no':
                break
      else:
            break

    print_tickets()
    print("\nWe will now redirect you to the payment portal."
      "\nThank You for choosing us.")

else:
    print("You can always come by later!")
页: [1]
查看完整版本: 订座系统