从用户处获取号码&打印最大值和最小值(不使用内置函数)

发布于 2024-09-28 05:16:48 字数 872 浏览 7 评论 0原文

我正在审查一个Python练习,它执行以下操作:

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

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

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

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

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

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

谢谢你!

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

  • 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
    using
    the built-in functions, max()
    and min().

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街道布景 2024-10-05 05:16:48

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

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

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
深海不蓝 2024-10-05 05:16:48

使用num = float(inp),您只需分配一个数字,并在每次分配新数字时覆盖它。您必须首先创建列表,然后每次向其中添加数字。像这样的事情:

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

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:

nums = []
while True:
  ...
  nums.append(float(inp))
宁愿没拥抱 2024-10-05 05:16:48
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)
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)
抹茶夏天i‖ 2024-10-05 05:16:48

试试这个代码:

 def max(data):
    l = data[0]
    s = data[0]
    for num in data:
        if num > l:
            l = num
        elif num < s:
            s = num
    return l, s
    print(max([0, 10, 15, 40, -5, 42, 17, 28, 75]))

Try this code :

 def max(data):
    l = data[0]
    s = data[0]
    for num in data:
        if num > l:
            l = num
        elif num < s:
            s = num
    return l, s
    print(max([0, 10, 15, 40, -5, 42, 17, 28, 75]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文