I have to show all fields with filter in numeric field, I have to choose which numeric field, set lower limit and higher eg: show all parts with price> = 45.50 and price
I have to show all fields with filter in numeric field, I have to choose which numeric field, set lower limit and higher eg: show all parts with price> = 45.50 and price
1) When you compare i
, you are comparing the index of the list, not the values. To compare the values, use lista[i]
;
2) Your function has a print(lista)
within for
, ie for each iteration of for, it will print something;
3) if i == filtro1 and i == filtro2:
this line did not make any sense;
4) if i >= filtro1 or i <= filtro2:
here is not or
, it is and
, in addition to being lista[i]
, not i
;
5) I recommend using list comprehension.
Fixing your function:
def mostrarCadastro2(lista, filtro1, filtro2):
lista_filtrada = []
for i in range(len(lista)):
if lista[i] >= filtro1 and lista[i] <= filtro2:
lista_filtrada.append(lista[i])
return lista_filtrada #ou print(lista_filtrada)
Other ways to do:
def mostrarCadastro2(lista, filtro1, filtro2):
return list(filter(lambda x: x >= filtro1 and x <= filtro2 , lista))
#Ou ainda
def mostrarCadastro2(lista, filtro1, filtro2):
return [ num for num in lista if (num >= filtro1 and num <= filtro2) ]
In case I have a list of list as follows:
cadastroPeca = [] item = []
fieldChoose = input ("Choose the field you left searching by typing by number:")
if campoEscolhido != "1" and campoEscolhido != "2" and campoEscolhido != "3":
print("\nOpção digitada não existe na lista acima! Por favor, escolha uma das opções abaixo.\n")
else:
campoEscolhido1 = int(campoEscolhido)
if campoEscolhido1 == 1:
print("Opção selecionada: ", campoEscolhido1)
filtroMenor = int(input("Informe o menor valor do item de pesquisa: "))
filtroMaior = int(input("Informe o maior valor do item de pesquisa: "))
mostrarCadastro2(cadastroPeca, filtroMenor, filtroMaior)
Introducing this error: Traceback (most recent call last): File "python", line 120, in File "python", line 27, in showCadastro2 TypeError: '> =' not supported between instances of 'list' and 'int'