我有一个任务,我应该创建一个票证,用户首先选择他们想要的票证类型,然后如果他们想要添加一个包(选项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,但都不起作用。有人知道我如何解决这个问题吗?谢谢
谢谢你的帮助!事实上,我尝试了另一种方法,根据选择将包设置为1或0,效果很好
现在,我的代码如下所示:
但是,它不像我想要的那样打印。例如,如果我选择1号客票并添加一个行李,则输出为:
但我只希望总数是700,我不希望在“Bag”之后的行中出现“0”。它仍然将膳食价格添加到总价中,“0”来自while循环之前的行。我把餐费和餐费的打印字符串放在选项3中,所以我不知道为什么它仍然添加餐费
因此,我希望收据上的内容是:
有什么办法可以解决这个问题吗?:)
你可以试试这个,如果这个对你有用的话
因为你只需要得到当前的选择。我认为你不需要这么做 添加到列表中。您可以分别存储餐和包的选项,最后您可以使用这两个选项创建一个新列表
相关问题 更多 >
编程相关推荐