使用%s,%d根据用户输入得分(python)分配通过/未通过成绩确定

时间:2017-07-14 23:38:33

标签: python

期望的结果: 输入测试分数(退出-99):55 输入测试分数(退出-99):77 输入测试分数(退出-99):88 输入测试分数(-99退出):24 输入测试分数(退出-99):45 输入测试分数(退出-99): - 99 55 77 88 24 45 P P P F F

处理完成,退出代码为0

到目前为止的代码:(除了Pass失败分配之外的其他工作)

Python程序要求用户输入添加到名为分数的列表中的分数。然后在该分数P下打印通过F失败。

scores = [] #list is initialized

while True:
    score = int(input("Enter a test score (-99 to exit): "))
    if score == -99:
        break
    scores.append(score)

def print_scores(): #accepts the list and prints each score separated by a space
    for item in scores:
        print(item, end = " ")      # or 'print item,'
print_scores()       # print output

def set_grades():       #function determines whether pass or fail
    for grade in scores:
        if score >= 50:
            print("P")
        else:
            print("F")
print(set_grades)

1 个答案:

答案 0 :(得分:1)

您正在考虑正确的思路,但您需要从顶部开始执行您的计划,并确保您的理由正确。

首先,您已经编写了程序来打印所有分数,然后再检查它们是否通过,这样您就可以得到一个数字列表,然后是P /列表F。这些需要一起发生才能正确显示。 另外,确保跟踪变量是什么;在你的上一个函数中,你试图使用“得分”,这已经不再存在了。 最后,我不确定您对%d或%s的具体要求是什么,但您可能正在寻找format()的命名参数,如下所示。

scores = [] #list is initialized

while True:
    score = int(input("Enter a test score (-99 to exit): "))
    if score == -99:
        break
    scores.append(score)

for item in scores:
    if item >= 50:
        mark = 'P'
    else:
        mark = 'F'

    print('{0} {1}'.format(item, mark))

我相信这是你正在寻找的。