[Python] How to print the tuples in a more user friendly way?

Discussão em 'Python' iniciado por Stack, Setembro 4, 2025.

  1. Stack

    Stack Membro Participativo

    First let me show you the code to explain what's the problem.

    #This is the second version of the marks analysis program
    L = [] #This list will contain both marks and subject
    S = []
    M = []#This list will only contain marks, for calculation purpose
    def help_dialogue():
    print("================HELP DIALOGUE=================")
    print("Press 1 to add marks to the list ")
    print("Press 2 to delete recent marks")
    print("Press 3 to display mark list")
    print("Press 4 to display total marks")
    print("Press 5 to display average marks")
    print("Press 6 to exit (Changes will not be saved")
    print("Press 7 to show help dialogue again")
    print("==============================================")
    def add_marks():
    sub = input("Enter Subject name : ")
    marks = int(input("Enter Marks : "))
    L.append((sub, marks))
    S.append(sub)
    M.append(marks)
    def print_marksheet():
    print(L)
    def average_marks():
    y = sum(M)/len(M)
    return y
    def total_marks():
    c = (sum(M))
    return c
    def delete_recent_marks():
    L.pop()
    M.pop()
    help_dialogue()
    while True:
    choice = int(input("Enter your choice: "))
    if choice == 1:
    add_marks()
    elif choice == 2:
    delete_recent_marks()
    elif choice == 3:
    print_marksheet()
    elif choice == 4:
    print(f"The total marks is {total_marks()}")
    elif choice == 5:
    print(f"The average marks is {average_marks()}")
    elif choice == 6:
    break
    elif choice == 7:
    help_dialogue()
    else:
    print("Invalid choice")
    print("Thank you for using this program")


    So in line 22

    def print_marksheet():
    print(L)


    It just plainly prints the tuples (As expected from the code). But i want the code to print it more user friendly way.

    Input:

    L = [('English', 78) , ('Mathematics', 99)]


    Output:-

    English => 78
    Mathematics => 99


    So , I tried at first using for loop;

    def print_marksheet():
    for subject in range(S):


    But here is the moment that i realize. That 'for' loop wasn't looping for the string and asked for integer. Here is the error I got:

    Traceback (most recent call last):
    File "C:\\Users\\v\*\*\*\\PycharmProjects\\PythonProject\\Marks analysis_v2.py", line 42, in \<module\>
    print_marksheet()
    \~\~\~\~\~\~\~\~\~\~\~\~\~\~\~^^
    File "C:\\Users\\v\*\*\*\*\\PycharmProjects\\PythonProject\\Marks analysis_v2.py", line 23, in print_marksheet
    for subject in range (L):
    \~\~\~\~\~\~^^^
    TypeError: 'list' object cannot be interpreted as an integer


    Then i searched in internet and found the command enumerate(). But i don't think so it would work for me. (And just in case this command works then please explain me how to use.)

    How to implement the user-friendly output?

    Continue reading...

Compartilhe esta Página