首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游乐园预订

游乐园预订
EN

Stack Overflow用户
提问于 2022-11-12 02:41:34
回答 1查看 64关注 0票数 0

该系统允许骑手在不需要等待的情况下预订一个位置。骑手只需在一个程序中输入一个名字,就可以预订一个位置。购买VIP通行证的骑手可以跳过普通骑手,直到最后一个VIP乘客排队。贵宾先上飞机。(考虑到迪士尼乐园的平均等待时间约为45分钟,这可能是一个有用的计划。)对于此系统,员工手动选择何时出动骑行(从而将下一个骑手从线路前部移除)。

代码语言:javascript
复制
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.\n\n')

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)

当我试图从线的前面移除骑手时,我一直在循环中得到一个错误。

EN

回答 1

Stack Overflow用户

发布于 2022-11-12 03:11:19

您的分派长度总是等于3,但您的列表可能不包含3项,因此您需要检查它。下面的代码修复了这个问题:

代码语言:javascript
复制
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.\n\n"
)

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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74410025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档