The code:
#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")
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 in a more user-friendly way.
Input:
L = [('English', 78) , ('Mathematics', 99)]
Output:
English => 78
Mathematics => 99
I tried at first using a for-loop:
def print_marksheet():
for subject in range(S):
But I realized that the for-loop wasn't looping for the string and asked for an 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
I searched the 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 how to use it.)
How can I implement the user-friendly output?
The general way to iterate in Python is:
for item in iterable:
...
Here, iterable
is your list L
. To take it one step further, since the items are tuples, you can decompose them into elements right away:
for sub, marks in L:
...
To print the data in a user-friendly way, use a f-string:
for sub, marks in L:
print(f"For subject {sub} the mark is {marks}")
enumerate
is used to assign a numeric index to each item. You could use it if you wanted your output to contain indexing, like this:
1. For subject English the mark is 78
2. For subject Mathematics the mark is 99