创建具有多个选项的票证

2024-10-02 00:27:36 发布

您现在位置: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.')

我希望根据用户选择的选项(也可以是一包或一顿饭,或两者兼而有之)输出如下:

目前您有: 登记0件行李 已登记0份膳食

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

如果我随后在下一个循环中选择选项3,则输出为:['1个包已注册''0个包已注册'],而不是仅['0个包已注册']

我曾尝试在特定索引上使用pop和insert,但都不起作用。有人知道我如何解决这个问题吗?谢谢


Tags: 用户选项pop票证bagelifappendwhile
2条回答

谢谢你的帮助!事实上,我尝试了另一种方法,根据选择将包设置为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

有什么办法可以解决这个问题吗?:)

你可以试试这个,如果这个对你有用的话

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

# 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)

相关问题 更多 >

    热门问题