首页 > 解决方案 > 如何继续从无限列表中调用

问题描述

我正在制作一个图表,从名为 的列表中调用人员readerlist。如何从无限列表中调用 1st 2nd 3rd... 而无需手动将其全部写出。



def isNumber(s):
    try:
        int(s)
        return True
    except ValueError:
        return False




readerlist = []
booklist = []




print ("welcome to the book club system".upper())
print()

while True :
    print ("\n------------------------------------")
    print ("|".ljust(12) + "OPTIONS MENU".ljust(23) + "|")
    print ("|".ljust(35) + "|")
    print ("| 1: Modify book total".ljust(35) + "|")
    print ("| 2: Displays people and books out".ljust(35) + "|")
    print ("| 3: Quit".ljust(35) + "|")
    print ("------------------------------------")
    
   
    while True :
        choice = input("\nEnter your selection: ")
        if choice == "1" or choice == "2" or choice =="3":
            break
        print ("Invalid option")

    #3 - quit
    if choice == "3" :
        break
    
    #2 - display totals so far
    elif choice == "2" :
        print ("\n\n")
        
        print ("".ljust(10) + "BOOKS READ".center(30)) 
        print ("NAME".ljust(15) + "BOOKS".ljust(15))
        print (readerlist[0].ljust(15) + str(booklist[0]).ljust(15))
        print (readerlist[1].ljust(15) + str(booklist[1]).ljust(15))
        print (readerlist[2].ljust(15) + str(booklist[2]).ljust(15))
        print (readerlist[3].ljust(15) + str(booklist[3]).ljust(15))
        print (readerlist[4].ljust(15) + str(booklist[4]).ljust(15))
        
    #1 - enter a new user, or update an existing user's total
    elif choice == "1":
        while True:
            name = input("Enter your name: ")
            if name != "<Empty>":
                readerlist.append(name)
                break
                
        while True:
            books = (input("Enter number of books read: "))
            if isNumber (books):
                books = int(books)
                break
            else:
                print("Invalid number")
               

        
print ("")
print ("Thank you for using the Library program")
input ("<Hit ENTER to exit>")

标签: python

解决方案


也许这就是你想要的:

    elif choice == "2" :
        print ("\n\n")
 
    print ("".ljust(10) + "BOOKS READ".center(30)) 
    print ("NAME".ljust(15) + "BOOKS".ljust(15))
    for i in readerlist:
        print(readerlist[i].ljust(15) + str(booklist[i]).ljust(15))

推荐阅读