创建具有多项选择的工单 Python

Create a ticket with multiple choices Python

我有一个任务,我应该创建一张票,用户首先选择他们想要的票,然后他们是否想添加行李(选项 1)或餐点(选项 2)。用户还可以选择删除包(选项 3)或一顿饭(选项 4),如果他们后悔自己的第一个选择。然后所有的选择都会打印在收据上。

我的问题是如何存储用户选择的选项。我创建了一个 while 循环,该循环一直运行到用户想要完成其工单为止。这是我的 while 循环:

while yourchoice != 5:

    if yourchoice == 1:
        addonebag = str('1 bag(s) registered')
        choiceslist.append(addonebag)

    elif yourchoice == 2:
        addonemeal = str('1 meal(s) registered')
        choiceslist.append(addonemeal)

    elif yourchoice == 3:
        removebag = str('0 bag(s) registered')
        choiceslist.pop(addonebag)
        choiceslist.append(removebag)

    elif yourchoice == 4:
        removemeal = str('0 meal(s) registered')
        choiceslist.pop(1,addmeal)
        choiceslist.insert(1,removemeal)

    else: 
        print('\n' 
          'Invalid option. Please try again.')

我希望输出看起来像这样,具体取决于用户选择的选项(它也可以说 1 袋或 1 餐或两者):

目前你有: 0 包已注册 0 餐已注册

问题是,当我创建此列表时,如果我在第一个循环中选择了选项 1,则输出为:['1 个包已注册']

如果我在下一个循环中选择选项 3,输出是:['1 个袋子已注册' '0 个袋子已注册'] 而不是 ['0 个袋子已注册' ].

我试过在特定索引上使用 pop 和 insert 但它不起作用。有谁知道我该如何解决这个问题?谢谢!

如果适合你,你可以试试这个。

因为你只需要得到当前的选择。我认为你不需要 附加到列表中。您可以分别存储餐食和行李的选择,最后您可以使用这两个选择创建一个新列表。

# In the beginning choices should be 0
bag_choice = '0 bag(s) registered'
meal_choice = '0 meal(s) registered'

while yourchoice != 5:

  if yourchoice == 1:
      bag_choice = '1 bag(s) registered'

  elif yourchoice == 2:
      bag_choice = '1 meal(s) registered'

  elif yourchoice == 3:
      bag_choice = '0 bag(s) registered'

  elif yourchoice == 4:
      meal_choice = '0 meal(s) registered'

  else: 
      print('\n' 
            'Invalid option. Please try again.')

#creating a list from bag_choice and meal_choice     
choice_list =  [bag_choice, meal_choice]
print(choice_list)

感谢您的帮助!我实际上尝试了另一种方法,我根据选择将包设置为 1 或 0,并且有效!

现在我的代码如下所示:

def tickettype():
    print('Ticket types: \n' 
          '1. Budget: 500 kr \n' 
          '2. Economy: 750 kr \n' 
          '3. VIP: 2000 kr \n')

def options():
    print('\nHere are your options: \n'
          '1. Add bag (max 1) \n'
          '2. Add meal (max 1) \n'
          '3. Remove bag \n'
          '4. Remove meal \n'
          '5. Finalize ticket \n')

tickettype()

tickettype1 = int(input('Choose your ticket type: '))

if tickettype1 == 1:
    ticket = 500

elif tickettype1 == 2:
    ticket = 750

elif tickettype1 == 3:
    ticket = 2000

options()

print('\n' 
    'Currently you have: \n'
      '0 bag(s) registered \n'
      '0 meal(s) registered \n')

yourchoice = int(input('Your choice: '))
bag = 0
meal = 0
addbag = 0
addmeal = 0

while yourchoice != 5:

    if yourchoice == 1:
        bag = 1
        bagprice = 200
        addbag = str('Bag    : 200')

    elif yourchoice == 2:
        meal = 1
        mealprice = 150
        addmeal = str('Meal   : 150')

    elif yourchoice == 3:
        bag = 0

    elif yourchoice == 4:
        meal = 0

    else: 
        print('\n' 
          'Invalid option. Please try again.')

    print('\n' 
        f'Currently you have: \n{bag} bag(s) registered \n{meal} meal(s) registered')

    options()

    yourchoice = int(input('Your choice: '))

# When the user press 5:

#EDITED AFTER THIS LINE


#print(f'\nReceipt:\nTicket : {ticket}\n{addbag}\n{addmeal} \nTotal: {ticket+bagprice+mealprice}'

#create a new variable to store Total price, adding mealprice or bagprice only if they are selected else 0 will be added
Total = (mealprice if meal == 1 else 0) + (bagprice if bag == 1 else 0) + ticket

print(f'\nReceipt:\nTicket:{ticket}')
#print bag only if it is selected, bag value will became 1 only if it is selected
if bag == 1:
  print(addbag)
# print meal only if it is selected, meal value will became 1 only if it is selected
if meal == 1:
  print(addmeal)
# print the Total price
print(f'Total:{Total}')

但是,它没有像我想要的那样打印。例如,如果我选择 1 号票并添加一件行李,则输出为:

Receipt:
Ticket : 500
Bag    : 200
0 
Total: 850

但我只希望总数为 700,而我不希望“Bag”后一行的“0”。它仍然将膳食价格添加到总价格中,而“0”来自 while 循环之前的行。我把餐价和餐的打印字符串放在选项3里,不知道为什么还是加了餐价?

所以我希望它在收据中的样子是:

Receipt:
Ticket : 500
Bag    : 200
Total: 700

关于如何解决这个问题的任何想法? :)