使用 %s、%d 根据用户输入分数 (Python) 分配通过/失败等级确定

  • 本文关键字:分配 失败 Python 用户 使用 python
  • 更新时间 :
  • 英文 :


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

进程已完成,退出代码为 0

到目前为止的代码:(除了通过失败分配外有效)

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)

你的想法是正确的,但你需要从顶部运行你的程序,并确保你的推理是正确的。

首先,你已经编写了程序来打印出所有的分数,然后再检查它们是否通过,所以你会得到一个数字列表,然后是一个P/F列表。这些需要一起发生才能正确显示。 另外,请确保跟踪什么变量是什么;在上一个函数中,您尝试使用不再存在的"score"。 最后,我不确定你到底在问 %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))

我相信这就是你要找的。

最新更新