Error when searching for a string

Asked

Viewed 36 times

0

Hey, how you doing? I’m having a problem executing this code. From the second "While True" the program can not find the written product. It was registered right above. I checked this code from tail to tail and found nothing that could be considered a mistake. If anyone can help me solve this jam, I’d appreciate it. I will make available the repl.it link so that you check the error that is happening. https://repl.it/repls/GoldFrivolousDragonfly

print('='*20, 'Cadastrador de produtos', '='*20)
def cadastros():
  cod = int(input('Digite o CÓDIGO do produto: '))
  item = input('Informe o produto que deseja cadastro: ')
  valor = input('Valor do produto: R$').replace(',',',')
  itens.append([cod,item, valor])
itens = []
while True:
  print('-='*20)
  deseja = str(input('Deseja cadastrar um item? S/N ')).upper().strip()
  if deseja == 'S':
    cadastros()
    print('Produto cadastrado com SUCESSO')
  elif deseja == 'N':
    break
  else:
    print('Entrada inválida')
    continue
print('Produtos cadastrados: {}'.format(itens))
print('='*70)
while True:
  x = 0
  op = input('Deseja procurar algum produto? [S/N]: ').upper().strip()
  if op == 'S':
    codPro = str(input('NOME DO PRODUTO: ')).upper().strip()
    for x in range(len(itens)):
      if codPro == itens[x][1]: # <--- Programa com erro na hora de procurar por nome do produto.## Cabeçalhos ##
        print('')
        print('Código: {}\nProduto: {}\nValor: R${}'.format(itens[x][0], codPro, itens[x][2]))
        print('')
      else: 
        print('Nome Invalido, tente novamente')
        continue
   ...

1 answer

1


Your problem is on the following line:










codPro = str(input('NOME DO PRODUTO: ')).upper().strip()

With the upper, you make all the letters of the input given uppercase. So, if your product has lowercase letters in the name, the comparison will not result in True. Try to register a product with the name capitalized and you will see that it works.

Solution: Take out the .upper().

Note that it is also not necessary to use str in input; output is already a string.

  • Thank you very much, all right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.