简体   繁体   English

如何停止一段时间或 if 语句多次运行?

[英]How to stop a while or if statement from running more than once?

Ask the user to enter the number of students in the class.要求用户在 class 中输入学生人数。 Thereafter ask the user to enter the student names and scores as shown.此后要求用户输入学生姓名和分数,如图所示。 Output the name and score of the student with the 2nd lowest score. Output 分数第二低的学生的姓名和分数。

NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students注意:您可以假设所有学生的分数都在 0 到 100 之间(含),并且至少有 2 名学生

NOTE: You may only use what has been taught in class so far for this assignment.注意:到目前为止,您只能使用 class 中教授的内容来完成此作业。 You are not allowed to use more advanced topics like lists, functions, dictionaries etc for this assignment.您不得在此作业中使用更高级的主题,如列表、函数、字典等。

I have gotten this far and I really don't know what to do know, is there an easies way to do this while staying inside the limits?我已经走了这么远,我真的不知道该怎么做知道,有没有一种简单的方法可以在保持限制的同时做到这一点?

Is there a way to stop a while or if a statement after it runs one time?有没有办法停止一段时间或运行一次之后的语句? My solution is not working.我的解决方案不起作用。 I submitted the code below:我提交了以下代码:

studentN = int(input('Enter the number of students: '))
i = 1
n = 1


studentna, studentsco = input('Please enter the student '+ str(n) +' name: '),int(input('Please the student ' + str(n) + ' score: '))

while i < studentN :
studentna2, studentsco2 = input('Please enter the student '+ str(n+1) +' name: '),int(input('Please the student ' + str(n+1) + ' score: '))
t = 1
if studentsco2 >= studentsco:
    t=1
    while t <= 1:
        studentna, studentsco = studentna2, studentsco2
        t += 1
        if studentN == 2:
            print (studentna, studentsco)
    while studentN == i+1:
        t = 1
        while t<= 1:
            if studentsco2 >= studentsco:
                studentna, studentsco = studentna2, studentsco2
    t += 1
elif studentsco2 <= studentsco:

    if studentN == 2:
        t = 1
        while t<= 1:
            t += 1
            print (studentna2, studentsco2)


    if studentN == i+1:
        t = 1
        while t<= 1:
            if studentsco2 <= studentsco:
                print (studentna, studentsco)
            t += 1
    else:
        studentna, studentsco = studentna2, studentsco2





print (studentna, studentsco)
n += 1
i += 1

All you have to do is to maintain the lowest and the secondlowest scores while the input is done:您所要做的就是在输入完成时保持最低和第二低的分数:

studentN = int(input('Enter the number of students: '))

i = 1
studentsco = 0
studentna = ''
lowest_studentsco = 101
lowest_studentna = ''
secondlowest_studentsco = 101
secondlowest_studentna = ''

while i <= studentN :

    studentna, studentsco = input('Please enter the student '+ str(i) +' name: '),int(input('Please the student ' + str(i) + ' score: '))

    if studentsco < lowest_studentsco:
        secondlowest_studentsco = lowest_studentsco
        secondlowest_studentna = lowest_studentna
        lowest_studentsco = studentsco
        lowest_studentna = studentna
    elif studentsco < secondlowest_studentsco:
        secondlowest_studentsco = studentsco
        secondlowest_studentna = studentna

    i += 1

print (secondlowest_studentna, secondlowest_studentsco)

A known techinque is to initialize the variables for the lowest values with the highest possible value +1 so that they are changed immediately.一种已知的技术是将变量初始化为具有最高可能值 +1 的最低值,以便立即更改它们。

The conditions are self explanatory.条件是不言自明的。 Beware of the order of the variable assignment.注意变量赋值的顺序。 Maybe you have learnt how to swap two variables a and b:) this is a little bit similar.也许你已经学会了如何交换两个变量 a 和 b:) 这有点相似。

I think this assignment is good for learning and not for complaining.我认为这个作业有利于学习而不是抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM