如何将这个“学生成绩”代码压缩为循环?

时间:2019-03-28 21:25:26

标签: python

我必须编写一个程序,该程序接受5个分数并确定每个分数的字母等级相等。结果将以表格格式打印。

我了解问题的出处以及所有其他我不打算中继的步骤,因为它们与我的问题无关。

我遇到的麻烦是如何将我的代码压缩为某种循环。

我觉得我所拥有的非常多余,应该有一种浓缩它的方法。 另外,我不允许使用此问题的列表。

# Letter grade constants
A_SCORE = 90
B_SCORE = 80
C_SCORE = 70
D_SCORE = 60
# Gather scores from user
num1 = int(input("Enter score 1: "))
if num1 >= A_SCORE:
    letter1 = "A"
elif num1 >= B_SCORE:
    letter1 = "B"
elif num1 >= C_SCORE:
    letter1 = "C"
elif num1 >= D_SCORE:
    letter1 = "D"
else:
    letter1 = "F"
num2 = int(input("Enter score 2: "))
if num2 >= A_SCORE:
    letter2 = "A"
elif num2 >= B_SCORE:
    letter2 = "B"
elif num2 >= C_SCORE:
    letter2 = "C"
elif num2 >= D_SCORE:
    letter2 = "D"
else:
    letter2 = "F"
num3 = int(input("Enter score 3: "))
if num3 >= A_SCORE:
    letter3 = "A"
elif num3 >= B_SCORE:
    letter3 = "B"
elif num3 >= C_SCORE:
    letter3 = "C"
elif num3 >= D_SCORE:
    letter3 = "D"
else:
    letter3 = "F"
num4 = int(input("Enter score 4: "))
if num4 >= A_SCORE:
    letter4 = "A"
elif num4 >= B_SCORE:
    letter4 = "B"
elif num4 >= C_SCORE:
    letter4 = "C"
elif num4 >= D_SCORE:
    letter4 = "D"
else:
    letter4 = "F"
num5 = int(input("Enter score 5: "))
if num5 >= A_SCORE:
    letter5 = "A"
elif num5 >= B_SCORE:
    letter5 = "B"
elif num5 >= C_SCORE:
    letter5 = "C"
elif num5 >= D_SCORE:
    letter5 = "D"
else:
    letter5 = "F"

# Print results
print("score 1: \t\t" + str(num1) + "\t\t" + letter1)
print("score 2: \t\t" + str(num2) + "\t\t" + letter2)
print("score 3: \t\t" + str(num3) + "\t\t" + letter3)
print("score 4: \t\t" + str(num4) + "\t\t" + letter4)
print("score 5: \t\t" + str(num5) + "\t\t" + letter5)

这是显示内容的示例。格式正确,我刚刚排除了表格标题。

Enter score 1: 75
Enter score 2: 88
Enter score 3: 91
Enter score 4: 80
Enter score 5: 85

score 1:        75      C
score 2:        88      B
score 3:        91      A
score 4:        80      B
score 5:        85      B

3 个答案:

答案 0 :(得分:0)

您的代码是使用循环的教科书案例。每当您发现自己编写的重复代码充满命令,每次仅更改次要细节时,请查看是否可以使用循环:

A_SCORE = 90
B_SCORE = 80
C_SCORE = 70
D_SCORE = 60

scores = []
grades = []

for n in range(1, 6):
    grade = int(input("Enter score {}: ".format(n)))
    grades.append(grade)

    if grade >= A_SCORE:
        scores.append('A')
    elif grade >= B_SCORE:
        scores.append('B')
    elif grade >= C_SCORE:
        scores.append('C')
    elif grade >= D_SCORE:
        scores.append('D')
    else:
        scores.append('F')

for n, (score, grade) in enumerate(zip(scores, grades), 1):
    print("score {}: \t\t".format(n) + str(grade) + "\t\t" + score)

输出:

Enter score 1: 75
Enter score 2: 88
Enter score 3: 91
Enter score 4: 80
Enter score 5: 85
score 1:        75      C
score 2:        88      B
score 3:        91      A
score 4:        80      B
score 5:        85      B

答案 1 :(得分:0)

是否没有作业约束来完全阻止列表并以困难的方式进行处理。

最好,我可以想象缩短代码是一种功能:

# Letter grade constants
A_SCORE = 90
B_SCORE = 80
C_SCORE = 70
D_SCORE = 60
# Gather scores from user

def getscore(num):
    if num >= A_SCORE:
        return "A"
    elif num >= B_SCORE:
        return "B"
    elif num >= C_SCORE:
        return "C"
    elif num >= D_SCORE:
        return "D"
    else:
        return "F"

num1 = int(input("Enter score 1: "))
num2 = int(input("Enter score 2: "))
num3 = int(input("Enter score 3: "))
num4 = int(input("Enter score 4: "))
num5 = int(input("Enter score 5: "))

letter1 = getscore(num1)
letter2 = getscore(num2)
letter3 = getscore(num3)
letter4 = getscore(num4)
letter5 = getscore(num5)

# Print results
print("score 1: \t\t" + num1 + "\t\t" + letter1)
print("score 2: \t\t" + num2 + "\t\t" + letter2)
print("score 3: \t\t" + num3 + "\t\t" + letter3)
print("score 4: \t\t" + num4 + "\t\t" + letter4)
print("score 5: \t\t" + num5 + "\t\t" + letter5)

请注意,也不需要将整数转换为字符串。打印功能会隐式执行此操作

答案 2 :(得分:0)

您可以使用bisect模块获取成绩。在这里,get_score将采用数字等级,并返回字母等级。

from bisect import bisect
def get_score(A):
    i = bisect([60, 70, 80, 90], A)
    return 'FDCBA'[i]

mm = []
for i in range(5):
    mm += [input('Enter score {}: '.format(i))]

for m in mm:
    print('score {}:  {}   {}'.format(i, m, get_score(int(m))))

#Outputs
Enter score 0: 75
Enter score 1: 88
Enter score 2: 91
Enter score 3: 80
Enter score 4: 85
score 4:  75   C
score 4:  88   B
score 4:  91   A
score 4:  80   B
score 4:  85   B