【问题标题】:How to traverse a list in python?如何遍历python中的列表?
【发布时间】:2022-11-17 11:00:49
【问题描述】:

我是 python 的新手,我正在尝试编写一个票务系统。我希望能够打印所有创建的票证。我确实附上了我的清单。但是,当我尝试打印它时,它确实显示了票证信息。有人告诉我我需要横向 Object_List 但我不知道该怎么做。我的代码如下:


#main.py
from ticket import Ticket
Object_List=[]  #Here you will create list that will keep track of a ll objects in main class
class MenuList(object):
    def menu():
        menu_options = {
            0: 'Exit',
            1: 'Submit help desk ticket' ,
            2: 'Show all tickets',
            3: 'Search ticket by ticket number',
            4: 'Re-open resolved ticket',
            5: 'Display ticket stats'
        }
        print(menu_options)
        option = int(input('Enter your choice: '))
        return option # this function will return choice value selected by user

def submit():
    staffname = input('Enter staff name: ')
    staffid = input('Enter staff ID: ')
    staffemail = input('Enter email address: ')
    issue = input('Description of issue: ')
    if issue == 'Password change':
        newpass = staffid[0:2] + staffname[0:3]
        print("Your new password is: " + newpass)
    ticketobject = Ticket(staffid, staffname, staffemail, issue)
    Object_List.append(ticketobject)
    # here append ticketobj in the Object_List so that each object will keep adding in the list

    Newticket = input('Do you have another problem to submit? (Y/N)')
    if Newticket == 'Y':
        return submit()
    else:
        return selection()

#define stat(object_List method here and travserse through the list and find stats similarly you can define other methods re-opnen, close etc.
def stats():
      for i in Object_List:
          print(i)





def selection():
    op=MenuList.menu()
    if op==0:
        print('Thanks for your submit')
        #enter code here...
    if op == 1:
        submit()
    elif op == 2:
        stats ()
    else:
        print('Incorrect input. Please select from the list: ')
        selection()
#menu will show on your output screen due to this method.....
selection()


#ticket.py
class Ticket():

    datal = [] # this is created in Ticket class
    def __init__(self, staffid, staffname, staffemail, issue):
         ticketnum = 2000
         self.staffid = staffid
         self.staffname = staffname
         self.staffemail = staffemail
         self.issue = issue
         self.status = 'open'
         self.answer = 'None'
         ticketlist = ("staff ID: " + staffid,
                      "staff name: " + staffname,
                      "staff email: " + staffemail,
                      "Description of issue: " + issue,
                      "Ticket status: " + self.status, "Ticket number: ", ticketnum, "Responds: " + self.answer )

         print(ticketlist)

【问题讨论】:

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


【解决方案1】:

您需要打印票据对象中的每条数据,尝试直接打印它只会打印内存地址位置。

def stats():
      for i in Object_List:
          print(i.staffid)
          print(i.staffname)
          # and so on, or you can put it in one large print statement and nicely formatted

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2016-08-27
    • 2011-09-14
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多