用户数据不会追加到列表中

时间:2018-12-15 16:48:59

标签: python python-3.x list loops listview

嘿,当我使用选项b在估算用户数据后没有显示任何内容时显示清单时,用户数据未追加到“数据库”列表中 id喜欢一些帮助,因为我看不到错误原因,因为for循环应该打印出数据

database = []


import sys

print()
print("Holiday Packages Database")
print('Packages Loaded:', len(database))
print("+-+-+-+-+-+-+-+-+-+-+-+-+")
print("Available Options")
print("a. Enter a new holiday package")
print("b. Display Inventory (all destinations)")
print('c. Search for a package (view details)')
print('d. Delete a holiday package')
print('e. Show all holiday packages with seasonal discount')
print('f. Show all holiday packages with no discount')
print('q. Quit')
print('Choose an option (a to f) or q to Quit ')

print()
menu = input("Enter a option from list above:")

#option a (Enter a new holiday package)
if menu == ('a'):
    ID = int(input("Enter the ID: "))
    city = input("Enter the Holiday destination city")
    country = input('Enter the Destination country')
    catering = int(input('Enter the amount of people to cater for'))
    night = int(input('Enter the number of nights you plan to stay'))
    price = int(input('Enter the price per person per night'))
    discount = input('Available or not available')
    database.append(ID, city, country, catering, night, price, discount)

#option b (Display inventory)
if menu == ('b'):
   for packages in range(0,len(database)):
        print(database[packages])

if menu != ("q"):
   print()
   print("Holiday Packages Database")
   print('Packages Loaded:', len(database))
   print("+-+-+-+-+-+-+-+-+-+-+-+-+")
   print("Available Options")
   print("a. Enter a new holiday package")
   print("b. Display Inventory (all destinations)")
   print('c. Search for a package (view details)')
   print('d. Delete a holiday package')
   print('e. Show all holiday packages with seasonal discount')
   print('f. Show all holiday packages with no discount')
   print('q. Quit')
   print('Choose an option (a to f) or q to Quit ')
   print()
   menu = input("Enter a option from list above:")

2 个答案:

答案 0 :(得分:0)

首先,您需要使用一个元组来进行如下添加:

database.append((ID, city, country, catering, night, price, discount))

第二,您需要使用while循环,以免对每个选项都重复一次。

第三,可以像这样完成列表的迭代:

for x in some_list:
    print(x)

完整脚本:

import sys

database = []

while True:
    print()
    print("Holiday Packages Database")
    print('Packages Loaded:', len(database))
    print("+-+-+-+-+-+-+-+-+-+-+-+-+")
    print("Available Options")
    print("a. Enter a new holiday package")
    print("b. Display Inventory (all destinations)")
    print('c. Search for a package (view details)')
    print('d. Delete a holiday package')
    print('e. Show all holiday packages with seasonal discount')
    print('f. Show all holiday packages with no discount')
    print('q. Quit')
    print('Choose an option (a to f) or q to Quit ')

    print()
    choice = input("Enter a option from list above:")

    #option a (Enter a new holiday package)
    if choice == 'a':
        ID = int(input("Enter the ID: "))
        city = input("Enter the Holiday destination city")
        country = input('Enter the Destination country')
        catering = int(input('Enter the amount of people to cater for'))
        night = int(input('Enter the number of nights you plan to stay'))
        price = int(input('Enter the price per person per night'))
        discount = input('Available or not available')
        database.append((ID, city, country, catering, night, price, discount))

    #option b (Display inventory)
    if choice == 'b':
       for package in database:
            print(package)

    if choice == 'q':
        sys.exit(0)

答案 1 :(得分:0)

您最好使用扩展,这应该为您完成工作:

database.extend([ID, city, country, catering, night, price, discount])