python - 如何继续从无限列表中调用
问题描述
我正在制作一个图表,从名为 的列表中调用人员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>")
解决方案
也许这就是你想要的:
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))
推荐阅读
- nestjs - 如何在变量中注入一些东西?
- javascript - React - 遍历 React.element 数组不起作用
- javascript - 成功调用后无法事件单击 SVG 元素
- javascript - Ant 设计表单问题
- javascript - 谷歌日历 - 事件日期,而不是一审日期
- javascript - 如何在 Chrome 移动设备的大表中加快 ContentEditable td 输入速度
- java - Spring Boot 是否支持服务器名称指示(SNI)?
- for-loop - 数据透视表和 FOR 子句
- python - Elasticsearch:查找一个字段与另一个字段的重叠
- typescript - Typescript 创建描述类型类型的属性,该类型基于抽象类型