Python 创建函数中的问题

Python 创建函数中的问题,python,python-3.x,Python,Python 3.x,这是我的第一个问题,我是编程新手,很抱歉有任何不协调! 我需要完成一个练习,其中包括创建一个函数来查找较高的数字,以及另一个函数来查找列表中较低的数字,但是当我打印结果时,它总是给我错误的答案 这是我的密码: lista_numeros = [1593, 200, 72, 8, 423, 2, 39, 293, 20120] def menor_numero(arg1): colector1 = arg1[0] print(coletor1) # - Checking v

这是我的第一个问题,我是编程新手,很抱歉有任何不协调!
我需要完成一个练习,其中包括创建一个函数来查找较高的数字,以及另一个函数来查找列表中较低的数字,但是当我打印结果时,它总是给我错误的答案

这是我的密码:

lista_numeros = [1593, 200, 72, 8, 423, 2, 39, 293, 20120]

def menor_numero(arg1):

    colector1 = arg1[0]

    print(coletor1) # - Checking value added to COLECTOR1

    for a in range(len(arg1)):

           print(arg1[a]) # - Checking if the A argument is running through the list.

           if colector1 < arg1[a]:

                     colector1 = arg1[a]

    return colector1


resultado2 = menor_numero(lista_numeros)

print("menor ", str(resultado2)) # Result is the last position of the list, must be the 6th position.
lista_numeros=[1593200728423,2,3929320120]
def菜单编号(arg1):
colector1=arg1[0]
打印(coletor1)#-检查添加到coletor1的值
对于范围内的(len(arg1)):
print(arg1[a])#-检查a参数是否在列表中运行。
如果colector1

非常感谢。

假设您的输入是列表,而不是字符串或某种类型,您可以使用min()/max()方法:

您可以在此处找到更多信息:

您的函数正在查找最大值而不是最小值。将
应该可以执行您想要的操作


Python还有内置的方法
min()
max()
,它们应该做你想做的事。

在Python中,缩进的第一步非常重要,它告诉Python执行代码的顺序,以及定义代码在循环中的位置等

现在你说你想做一个函数,从另一个函数的输出中找出最小和最大的数字,为此我假设这个输出是一个列表

请参阅下面的代码和注释

Mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9] #assume this is output from other funtion

def func(alist): #define function
    collector1 = 100 #keeping your collector idea
    for i in alist: #iterate through input
        if i < collector1: #check if the item you are currently looking at is smaller than the item currently stored in collector
            collector1 = i #if is smaller overwitre colletor with new item
    print(collector1) #after iterating through all items in input print final value of colletor

func(Mylist) #call function with input
只要改变这个,

    if i > collector1:
现在它会发现最大的输入,所以输出现在是

9
编辑:如果您正在查找最大的起始收集器1,如果您正在查找最大的起始收集器1,则在=1。

#!蟒蛇3
#! python3

import random

numbers = []
max = 1000
min = 0

for i in range(40):
    numbers.append(random.randint(min,max))

maxNum = min
minNum = max

for num in numbers:
    if num > maxNum:
        maxNum = num
    elif num < minNum:
        minNum = num

print(maxNum, minNum)
随机输入 数字=[] 最大值=1000 最小值=0 对于范围(40)内的i: 数字.append(random.randint(最小值,最大值)) 最大值=最小值 最小值=最大值 对于数字中的num: 如果num>maxNum: maxNum=num elif num
这是我的代码,我使用python中的随机库生成一个随机数字列表,然后将max设置为该列表中的最大数字

下面的for循环生成40个随机数并将它们附加到我的列表中

然后我将maxNum设置为等于零,因此所有值都将大于它,因此初始值不会影响结果,然后我将minNum设置为等于max,因此每个值都将小于它

最后一个代码块在数字列表中循环,并将每个数字与当前的maxNum和minNum变量进行比较,以查看该数字是大于最大值还是小于最小值。如果是,maxNum(或minNum)数字将被更新,代码将移到下一个数字

最后一条语句打印或显示最小值和最大值


我不知道你在学什么课程,但我建议你熟悉这段代码并了解它在做什么,因为它非常基本,你将来遇到的东西会更难。

似乎你的缩进有点不对劲,粘贴的代码与你正在运行的代码相等?您能添加您的输出吗?您确定您的
arg
包含数字而不是文本吗?我们无法从您发布的代码中看到。请使您的代码真实,包括您的数据、预期输出和收到的任何错误消息。谢谢,我已经编辑过了!对不起!谢谢你的提醒!哦,我明白了!知道了!非常感谢你,瑞安!嗨,布兰登!我不知道那种方法!非常感谢您的分享,非常感谢!别担心!请随意将此答案设置为“已回答”,然后投票!。这对我帮助很大!:)
9
#! python3

import random

numbers = []
max = 1000
min = 0

for i in range(40):
    numbers.append(random.randint(min,max))

maxNum = min
minNum = max

for num in numbers:
    if num > maxNum:
        maxNum = num
    elif num < minNum:
        minNum = num

print(maxNum, minNum)