Python 从用户获取数字并打印最大值和最小值(不使用内置函数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3938062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:25:26  来源:igfitidea点击:

get numbers from user & print maximum and minimum (w/o using built-in function)

pythonlistmaxminimum

提问by digging

I'm reviewing a python exercise which does the following :

我正在执行以下操作的 python 练习:

  • reads list of numbers til "done" gets entered.

  • When "done" is inputted, print largest and smallest of the numbers.

  • And it should be without directly usingthe built-in functions, max() and min().

  • 读取数字列表直到输入“完成”。

  • 当输入“done”时,打印最大和最小的数字

  • 它应该不直接使用内置函数 max() 和 min()。

Here is my source. Traceback says, "'float' object is not iterable"

这是我的来源。Traceback 说,“'float' 对象不可迭代”

I think my errors are coming from not using the list properly to calculate smallest and largest. Any tips and help will be greatly appreciated!

我认为我的错误来自没有正确使用列表来计算最小和最大。任何提示和帮助将不胜感激!

while True:
    inp = raw_input('Enter a number: ')
    if inp == 'done' : 
        break

    try:
        num = float(inp)
    except:
        print 'Invalid input'
        continue                            

numbers = list(num)
minimum = None       
maximum = None

for num in numbers :                          
    if minimum == None or num < minimum :
        minimum = num

for num in numbers :        
    if maximum == None or maximum < num :
        maximum = num

print 'Maximum:', maximum
print 'Minimum:', minimum

Thank you!

谢谢!

回答by Frank

With num = float(inp)you only assign a single number and overwrite it each time a new one is assigned. You have to create the list first, then add numbers to it each time. Something like this:

有了num = float(inp)你只分配一个号码,每一个新的分配时间覆盖它。您必须先创建列表,然后每次都添加数字。像这样的东西:

nums = []
while True:
  ...
  nums.append(float(inp))

回答by Mike DeSimone

You shouldn't need a list. You should only need to keep track of the current minimum and maximum as you go.

你不应该需要一个列表。您应该只需要随时跟踪当前的最小值和最大值。

minimum = None
maximum = None

while True:
    inp = raw_input('Enter a number: ')
    if inp == 'done': 
        break

    try:
        num = float(inp)
    except:
        print 'Invalid input'
        continue                            

    if minimum is None or num < minimum:
        minimum = num

    if maximum is None or num > maximum:
        maximum = num

print 'Maximum:', maximum
print 'Minimum:', minimum

回答by RakeshKirola

input_set = []
input_num = 0

while (input_num >= 0):

    input_num = int(input("Please enter a number or -1 to finish"))
    if (input_num < 0):
        break
    input_set.append(input_num)

print(input_set)

largest = input_set[0]
for i in range(len(input_set)):

    if input_set[i] > largest:
        greatest = input_set[i]

print("Largest number is", greatest)



smallest = input_set[0]
for i in range(len(input_set)):

    if input_set[i] < largest:
        smallest = input_set[i]

print("Smallest number is", smallest)