编写一个程序来解决以下问题。在物理测试中,十年级的学生给出了 15 个例子。如果正确解决了一半以上的示例,则进行测试。学生获得了多少学分?如果至少一名学生正确解决了所有问题,则打印“是”,否则打印“否”。该程序接收 N 类学生人数(1 ≤ N ≤ 30)作为输入,然后为每个学生输入正确解决示例的数量。
这是比较 print(temp == 15) 显示 True 时的程序 在条件块中它不起作用(!)
N = int(input())
i = 0
count = 0
msg = "YES"
while i < N:
temp = int(input())
if temp == 15:
msg = "YES"
else:
msg = "NO"
if temp >= 8 and temp < 15:
count = count + 1
i=i+1
print(count)
print(msg)
谢谢大家的回答!结果是这样的:
N = int(input())
i = 0
count = 0
array = []
while i < N:
temp = int(input())
if temp >= 8 and temp <= 15:
count = count + 1
if temp == 15:
array.append(temp)
i=i+1
print(count)
if len(array) > 0:
print("YES")
else:
print("NO")
你的逻辑是错误的。如果您需要确定至少一个学生已解决 15 个问题,则无需为下一个学生将消息重置为“否”。初始值只需设置为“NO”即可
除了过度分配的问题之外,
msg您还错误地计算了获得学分的学生人数,因为该变量count没有考虑到回答所有问题的学生。其他注意事项:
|(或)设置此标志是有意义的while在这种情况下,最好将循环替换为更简洁的循环for i in range(N)if可以以缩写形式重写if 8 <= temp <= 15:你会得到类似的东西: