debugging 我如何调试这段代码?我正在做一个评估,但什么都搞不清楚

ezykj2lf  于 2022-11-14  发布在  其他
关注(0)|答案(2)|浏览(158)
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))

实在想不出什么
评估具有以下指导原则。
要求用户在列表/数组中输入五个科目的分数。
程序必须确保标记在0和100之间
显示输入的标记列表/数组。
求出列表中所有分数的总和(所有五个科目),并将输出显示为:
您的分数总和为:[金额]
求出列表中所有分数的平均值(所有五名受试者),并将输出显示为:
您的平均分数为:[平均分]

ni65a41a

ni65a41a1#

如果您说的“debug”是指检查值并验证它,那么您可以编写一些单元测试另一种方法是安装ipdb并设置ipdb.set_trace()

wwodge7n

wwodge7n2#

不如这样吧:

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}")

相关问题