Python序列,用于统计标记大于和小于50的受试者的数量。50分及以下视为不及格。
样本输入
没有。受试者数量:
5
输入标记:
65
51
34
46
54
样本输出
次级通过:3
Sub失败:2
我的程序:
x=int(input("No. of subjects: n"))
print("Enter marks:")
for i in range(x):
y=[int(input())]
count=0
h=0
for j in y:
if j>50:
count=count+1
if j<50:
h=h+1
print("Sub passed: " + str(count))
print("Sub failed: " + str(h))
上述程序在获得输入后不会返回任何值。
它可以更整洁一点:
x=int(input("No. of subjects: n"))
print("Enter marks:")
y= [int(input()) for i in range(x)]
passed = len([y_p for y_p in y if y_p > 50])
not_passed = len([y_n for y_n in y if y_n< 50])
print(passed) #According to question would print: 3
print(not_passed) #According to question would print: 2
首先,您创建了一个列表,其中包含具有列表理解能力的所有输入。然后再列出另一个列表,其中包含已通过和未通过的条件。
您需要在循环之外定义count
和h
:
x=int(input("No. of subjects: n"))
print("Enter marks:")
count = 0
h = 0
for i in range(x):
y=[int(input())]
for j in y:
if j>50:
count=count+1
if j<50:
h=h+1
print("Sub passed: " + str(count))
print("Sub failed: " + str(h))