该系统允许乘客在排队时预订座位,而无需实际等待。乘客只需在程序中输入一个名字就可以预订座位。购买VIP通行证的乘客可以跳过普通乘客,直到排队的最后一个VIP乘客。贵宾先上车。(考虑到迪士尼乐园的平均等待时间约为45分钟,这可能是一个有用的项目。)对于这个系统,员工手动选择何时调度(从而将下一个乘客从队列的前面移除)。
riders_per_ride = 3 # Num riders per ride to dispatch
line = [] # The line of riders
num_vips = 0 # Track number of VIPs at front of line
menu = ('(1) Reserve place in line.n' # Add rider to line
'(2) Reserve place in VIP line.n' # Add VIP
'(3) Dispatch riders.n' # Dispatch next ride car
'(4) Print riders.n'
'(5) Exit.nn')
user_input = input(menu).strip().lower()
while user_input != '5':
if user_input == '1': # Add rider
name = input('Enter name:').strip().lower()
print(name)
line.append(name)
elif user_input == '2': # Add VIP
#print('FIXME: Add new VIP')
# Add new rider behind last VIP in line
name = input('Enter name:').strip().lower()
# Hint: Insert the VIP into the line at position num_vips.
line.insert(num_vips,name)
#Don't forget to increment num_vips.
num_vips += 1
elif user_input == '3': # Dispatch ride
#print('FIXME: Remove riders from the front of the line.')
# Remove last riders_per_ride from front of line.
for i in range(riders_per_ride):
line.pop(0)
# Don't forget to decrease num_vips, if necessary.
#num_vips -= 1
elif user_input == '4': # Print riders waiting in line
print(f'{len(line)} person(s) waiting:'.format(len(line)), line)
else:
print('Unknown menu option')
user_input = input('Enter command: ').strip().lower()
print(user_input)
当我试图从队列的前面移除乘客时,循环中总是出现错误。
您的分派长度总是等于3,但您的列表可能不包含3个项目,因此您需要检查它。下面的代码修复了这个问题:
riders_per_ride = 3 # Num riders per ride to dispatch
line = [] # The line of riders
num_vips = 0 # Track number of VIPs at front of line
menu = (
"(1) Reserve place in line.n" # Add rider to line
"(2) Reserve place in VIP line.n" # Add VIP
"(3) Dispatch riders.n" # Dispatch next ride car
"(4) Print riders.n"
"(5) Exit.nn"
)
user_input = input(menu).strip().lower()
while user_input != "5":
if user_input == "1": # Add rider
name = input("Enter name:").strip().lower()
print(name)
line.append(name)
elif user_input == "2": # Add VIP
# print('FIXME: Add new VIP')
# Add new rider behind last VIP in line
name = input("Enter name:").strip().lower()
# Hint: Insert the VIP into the line at position num_vips.
line.insert(num_vips, name)
# Don't forget to increment num_vips.
num_vips += 1
elif user_input == "3": # Dispatch ride
# print('FIXME: Remove riders from the front of the line.')
# Remove last riders_per_ride from front of line.
for i in range(min(riders_per_ride, len(line))):
line.pop(0)
# Don't forget to decrease num_vips, if necessary.
# num_vips -= 1
elif user_input == "4": # Print riders waiting in line
print(f"{len(line)} person(s) waiting:".format(len(line)), line)
else:
print("Unknown menu option")
user_input = input("Enter command: ").strip().lower()
print(user_input)