使用后端功能返回Python中所有列表值的字符串
我有两个文件为 frontend.py 和 backend.py ,并希望前端致电后端,以返回一个字符串单字符串。后端不得与用户完全互动(无打印,stdout,readline),并且必须使用时循环进行列表的解压缩。
运行前端时,我会遇到以下错误。从我的理解来看,它告诉我,通过的索引在列表本身中没有相应的索引。这对我来说没有意义,因为我在执行之前打印了这些值,并且它们是准确的。
错误
Traceback (most recent call last):
File "frontend.py", line 41, in <module>
main()
File "frontend.py", line 37, in main
print(backend.output_all(colours, categories, prices))
File "backend.py", line 26, in output_all
message += "Colour: " + list1[i]
IndexError: list index out of range
frontend
import sys
import backend
def main():
colours = backend.init_data_structure()
categories = backend.init_data_structure()
prices = backend.init_data_structure()
sys.stdout.write("MAIN MENU\n")
sys.stdout.write(backend.menu())
choice = sys.stdin.readline().strip().upper()
while choice != "Q":
if choice == "A":
sys.stdout.write("What is the colour of the item?\n")
colour = sys.stdin.readline().strip().upper()
backend.add_record(colours, colour)
sys.stdout.write("What category does the item belong to?\n")
sys.stdout.write("[1] Trousers\n")
sys.stdout.write("[2] Skirts\n")
sys.stdout.write("[3] Blouses\n")
sys.stdout.write("[4] Sales\n")
category = int(sys.stdin.readline())
backend.add_record(categories, category)
sys.stdout.write("What is the price of the item?\n$")
price = float(sys.stdin.readline())
backend.add_record(prices, price)
sys.stdout.write("How would you like to proceed?\n")
sys.stdout.write(backend.menu())
choice = sys.stdin.readline().strip().upper()
elif choice == "D":
print(backend.output_all(colours, categories, prices))
main()
后端
def init_data_structure():
return []
def add_record(list_name, data):
list_name.append(data)
def list_length(data_structure):
length = len(data_structure)
return int(length)
def get_value_at_index(list_name, index):
return data_structure[index]
def output_all(list1, list2, list3):
message = ""
l = list_length(list1)
i = 0
while i <= l:
message += "Record # " + str(i)
message += "-------------------"
message += "Colour: " + get_value_at_index(list1, i)
message += "Category: " + str(get_value_at_index(list1, i))
message += "Price: " + str(get_value_at_index(list1, i))
message += "\n"
i += 1
return message
def menu():
menu = "---------------\n"
menu += "[A]dd Record\n"
menu += "[D]isplay All Records\n"
menu += "[Q]uit\n"
menu += "---------------\n"
menu += "Your Selection: "
return menu
我尝试将代码全部移至前端,已将其重写为输出而没有循环,以及许多其他修改为尝试找到问题。
我是编程的新手,这是为了进行作业,这将为我学习我在这里出错的地方会做很多事情。我一直能够调试和找到错误,并使用stackoverflow(例如stackoverflow)向他们学习,但这让我很难过。
I have two files being frontend.py and backend.py and want the front end to call the backend to return a string containing the output of all values across three lists in a single string. The back end must not interact with the user at all (no print, stdout, readline) and the unpacking of the list must be done using a while loop.
When running the frontend I am getting the below error. From my understanding, it is telling me that the index being passed in does not have a corresponding index in the list itself. It doesn't make sense to me as I have printed out the values before they execute and they are accurate.
ERROR
Traceback (most recent call last):
File "frontend.py", line 41, in <module>
main()
File "frontend.py", line 37, in main
print(backend.output_all(colours, categories, prices))
File "backend.py", line 26, in output_all
message += "Colour: " + list1[i]
IndexError: list index out of range
FRONTEND
import sys
import backend
def main():
colours = backend.init_data_structure()
categories = backend.init_data_structure()
prices = backend.init_data_structure()
sys.stdout.write("MAIN MENU\n")
sys.stdout.write(backend.menu())
choice = sys.stdin.readline().strip().upper()
while choice != "Q":
if choice == "A":
sys.stdout.write("What is the colour of the item?\n")
colour = sys.stdin.readline().strip().upper()
backend.add_record(colours, colour)
sys.stdout.write("What category does the item belong to?\n")
sys.stdout.write("[1] Trousers\n")
sys.stdout.write("[2] Skirts\n")
sys.stdout.write("[3] Blouses\n")
sys.stdout.write("[4] Sales\n")
category = int(sys.stdin.readline())
backend.add_record(categories, category)
sys.stdout.write("What is the price of the item?\nquot;)
price = float(sys.stdin.readline())
backend.add_record(prices, price)
sys.stdout.write("How would you like to proceed?\n")
sys.stdout.write(backend.menu())
choice = sys.stdin.readline().strip().upper()
elif choice == "D":
print(backend.output_all(colours, categories, prices))
main()
BACKEND
def init_data_structure():
return []
def add_record(list_name, data):
list_name.append(data)
def list_length(data_structure):
length = len(data_structure)
return int(length)
def get_value_at_index(list_name, index):
return data_structure[index]
def output_all(list1, list2, list3):
message = ""
l = list_length(list1)
i = 0
while i <= l:
message += "Record # " + str(i)
message += "-------------------"
message += "Colour: " + get_value_at_index(list1, i)
message += "Category: " + str(get_value_at_index(list1, i))
message += "Price: " + str(get_value_at_index(list1, i))
message += "\n"
i += 1
return message
def menu():
menu = "---------------\n"
menu += "[A]dd Record\n"
menu += "[D]isplay All Records\n"
menu += "[Q]uit\n"
menu += "---------------\n"
menu += "Your Selection: "
return menu
I have tried moving the code all to the front end, have rewritten it to output without a loop, and many other modifications to try and locate the issue.
I am new to programming and this is for an assignment, it would do a lot for my learning to know where I've gone wrong here. I have always been able to debug and locate my errors and learn from them using online resources like StackOverflow but this one has me stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@mushroomator谢谢您,这是因为我正在使用&lt; = list_length而不是lt; list_length。
@mushroomator thank you, it was because I was using <= list_length and not < list_length.