基本的while循环程序

时间:2018-04-30 12:03:43

标签: python while-loop

上个月我一直在自学Python。我接受了编程课程的采访,需要帮助编写一个使用while循环的程序。 任务如下:

编写一个程序,要求用户输入5个数字,并输出这些数字中最大的数字和最小数字。因此,例如,如果用户输入数字2456 457 13 999 35,则输出为:

最大的数字是2456 最小的数字是13

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

你去吧

numbers = [] #this will be the list in which we will store the numbers
while len(numbers) < 5:  #len return the length of your list, we want our while loop to repeat 5 times
    numbers.append(double(input("enter number: "))) # adds the inputed number to the list
print("The largest number is",max(numbers),"The smallest number is",min(numbers))

答案 1 :(得分:1)

下次您提问时,请至少包含一些进度,以便每个人都愿意帮助您。 学习语言时,良好的编码风格也很重要。 这是给你的答案。我希望你能学到一些东西。您还可以尝试将max()和min()与列表一起使用。

input_times = 5
max_num, min_num = -float('inf'), float('inf')
while input_times:
    input_times -= 1
    try:
        num = int(input('Please enter a number:\n'))
        if num < min_num:
            min_num = num
        if num > max_num:
            max_num = num
    except:
        print("Please input an integer!")
        input_times += 1
print('The largest number is {max_num} The smallest number is {min_num}'.format(
       max_num = max_num, min_num = min_num))

答案 2 :(得分:0)

<app-heroes></app-heroes>