Home > Net >  How do I debug this code? I am doing an assessment and cannot figure anything out
How do I debug this code? I am doing an assessment and cannot figure anything out

Time:11-12

print("please enter your 5 marks below") 

 

#read 5 inputs 

mark1 = int(input("enter mark 1: ")) 

mark2 = int(input("enter mark 2: ")) 

mark3 = int(input("enter mark 3: ")) 

mark4 = int(input("enter mark 4: ")) 

mark5 = int(input("enter mark 5: ")) 

 

#create array/list with five marks 

marksList = [mark1, mark2, mark3, mark4, mark5] 

 

#print the array/list 

print(marksList) 

 

#calculate the sum and average 

sumOfMarks = sum(marksList) 

averageOfMarks = sum(marksList)/5 

 

#display results 

print("The sum of your marks is: " str(sumOfMarks)) 

print("The average of your marks is: " str(averageOfMarks)) 

Couldn't really come up with anything

The assessment has the following guidelines.

Ask the user to input the marks for the five subjects in a list/array.

The program must ensure that the marks are between 0 and 100

Display the list/array of marks entered.

Find the sum of all the marks in the list (all five subjects) and display the output as:

The sum of your marks is: [sum]

Find the average of all the marks in the list (all five subjects) and display the output as:

The average of your marks is: [average mark]

CodePudding user response:

If when you said "debug", you are referring to inspect the values and validate it, then you can write some unit tests another way is to install ipdb and set a ipdb.set_trace()

CodePudding user response:

How about this:

N = 5
print(f"please enter your {N} marks below") 

# read inputs
def checked_input(tip: str) -> int:
    try:
        num = int(input(tip))
        assert 0 <= num <= 100
    except (ValueError, TypeError, AssertionError):
        print('mark must between 0 and 100!')
        return checked_input(tip)
    else:
        return num

marks = [checked_input(f'enter mark {i}: ') for i in range(1, N 1)]

# print the array/list 
print(marks) 

# calculate the sum and average 
total = sum(marks) 
average = total / N

# display results 
print(f"The sum of your marks is: {total}") 
print(f"The average of your marks is: {average}") 
  • Related