【问题标题】:How do I find the second lowest value of inputs using while loops in python?如何在 python 中使用 while 循环找到输入的第二低值?
【发布时间】:2019-10-25 19:11:51
【问题描述】:

students = int(input('输入学生人数:')) 计数器 = 1

while(计数器

name = input('Please enter student ' + str(counter) + ' name:')
score = int(input('Please enter student ' + str(counter) + ' score:'))

lowest = 100
secondlowest = 100

if (score < lowest):
    secondlowest = lowest
    lowest = score

elif score > lowest and score < secondlowest:
    secondlowest = score

counter = counter + 1

print("2nd lowest student is " + name + " with score " + str(secondlowest))

【问题讨论】:

  • 您可以简单地使用一个额外的变量。当你找到一个最小的数字时,最旧的就是第二个,如果你找到另一个,第一个到第二个,这将替换第一个

标签: python loops while-loop


【解决方案1】:

您只需要两个变量来跟踪两个最低分数

  • 如果最新分数低于最低分数,则更新该分数并将第二低设置为第一

  • 否则,如果最新分数低于当前的第二低,只需使用新值更新即可。

【讨论】:

  • 我该怎么做?
  • 你能不能把它放在我不明白的代码中谢谢你
  • @Iambadatpython - 我的答案是伪代码,以帮助您学习,两个要点是您需要的 if 语句和 elif 语句,“最新分数”将是您当前的@ 987654321@变量,你只需要添加一个lowest_scoresecond_lowest_score变量
  • 是的,但是 if 语句看起来像什么,我不知道该怎么做
  • 对不起我超级笨
【解决方案2】:
while True: # Use a "while|true|break to ensure input is correct
    try: 
        students = int(input('Enter number of students: ')) # I fnot an int, raise error
        break # Stops while loop
    except: # Catch not an int error
        print("Input 'students ' must be an integer") # Print explanation, go to next while

counter = 0
lowest_score  = None # None versus high number to avoid limits
lowest_name   = None
second_lowest_score = None
second_lowest_name  = None

testlist = [] # Just for checking alidity of order. Delete before submission

while (counter < students):
    while True: # Get new input
        try: 
            score = int(input('Please enter student {} score:'.format(counter)))
            break
        except: 
            print("Score must be an integer...")

    name = input('Please enter student  name:'.format(counter))

    testlist.append(score) # Just for checking, Delete       

    # Handle first loop
    if lowest_score is None: 
        lowest_score = score
        lowest_name = name
        continue

    # Handle first loop
    if second_lowest_score is None:
        if score > lowest_score:
            second_lowest_score = score
            second_lowest_name = name
        else:
            second_lowest_score = lowest_score
            second_lowest_name = lowest_name
            lowest_score = score
            lowest_name = name
        continue # next iteration of main while

    if (score < second_lowest_score) :
        if (score >= lowest_score) :
            second_lowest_score = score
            second_lowest_name = name
        else:
            second_lowest_score = lowest_score
            second_lowest_name = lowest_name

    if score < lowest_score:
        lowest_score  = score
        lowest_name = name

    counter = counter + 1

print('2nd lowest student is ',
      name, 
      'with score', 
      second_lowest_score)
print(sorted(testlist))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多