使用后端功能返回Python中所有列表值的字符串

发布于 2025-01-18 12:36:56 字数 3148 浏览 0 评论 0原文

我有两个文件为 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?\n
quot;)
            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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-01-25 12:36:56

@mushroomator谢谢您,这是因为我正在使用&lt; = list_length而不是lt; list_length。

def output_all(list1, list2, list3):
message = ""
l = list_length(list1)
i = 0
while i < l:
    message += "Record # " + str(i) + "\n"
    message += "-------------------\n"
    message += "Colour: " + get_value_at_index(list1, i) + "\n"
    message += "Category: " + str(get_value_at_index(list2, i)) + "\n"
    message += "Price: $" + str(get_value_at_index(list3, i)) + "\n"
    message += "\n"
    i += 1

return message

@mushroomator thank you, it was because I was using <= list_length and not < list_length.

def output_all(list1, list2, list3):
message = ""
l = list_length(list1)
i = 0
while i < l:
    message += "Record # " + str(i) + "\n"
    message += "-------------------\n"
    message += "Colour: " + get_value_at_index(list1, i) + "\n"
    message += "Category: " + str(get_value_at_index(list2, i)) + "\n"
    message += "Price: 
quot; + str(get_value_at_index(list3, i)) + "\n"
    message += "\n"
    i += 1

return message
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文