Treating a string input when I hit enter without typing anything

Asked

Viewed 122 times

-1

Talk to the guys! If anyone can help me?

When he asks me if I want to continue and I press enter without typing anything he lets me proceed. I wish that when I hit enter without typing anything it won’t let me continue, only if I type 'Ss' or 'Nn'. How do I handle this blank string? Thanks...

soma = media = cont =  0
maior = menor = 0
r = 'Ss'

while r != 'Nn':
    while True:
        try:
            n = float(input('Digite um número inteiro: '))
            break
        except ValueError:
            print('Entrada Inválida!')
    soma += n
    cont += 1
    if cont == 1:
        maior = n
        menor = n
    if n > maior:
        maior = n
    elif n < menor:
        menor = n
    r = str(input('Quer continuar? S/N? ')).strip()
    while r not in 'Ss' and r not in 'Nn':
        r = str(input('Quer continuar? S/N?' ')).strip()
    if r not in 'Ss':
        r = 'Nn'
media = soma / cont

print('\033[36m<=>\033[m'*10)
print('Foram digitados \033[32m{}\033[m números e a \033[32mmédia\033[m entre eles foi de \033[32m{:.2f}\033[m.'.format(cont, media))        
print('O \033[32mmaior\033[m número digitado foi \033[32m{}\033[m.'.format(maior))
print('O \033[32mmenor\033[m número digitado foi \033[32m{}\033[m.'.format(menor))
  • while r not in 'Ss' and r not in 'Nn': realize that it is an and, not an or

  • Thanks. The point is that when I don’t digitize anything it proceeded with the code. I’d like to treat that. I’ve used the or else it stays inside the loop forever. The question here is to treat the string in white, otherwise I’m wrong.

2 answers

0


The solution I found was to split the script into two functions continuar and fazer.

I suggest that first test in this way without making any changes and then increment the method fazer.

resposta = True


def continuar():    

    resposta = input('\nQuer continuar? [S/N] ')    
    resposta = resposta[0:1].lower() # Simplificando a resposta em apenas um caractere minúsculo

    if resposta == "s":
        return True

    elif resposta == "n":        
        return False

    else:
        continuar()


def fazer():

    n = 0

    try:
        n = int(input('\nDigite um número inteiro: '))        
        return n

    except ValueError:        
        print('Entrada Inválida!')        
        return False


while resposta != False:

    n = fazer()

    if n: # Se n não for False
        print("Saída:", n)        
        resposta = continuar()

    elif resposta: # Se resposta não for False        
        fazer()
  • I tested without change and it worked. Now it is only necessary to create the function to do. Thanks for the strength.

  • I am happy to help, please mark my answer as right . Hug and see you next time!

  • Absolutely. It’s my first post here. I’m still learning. I just haven’t been able to increase the function yet, but I get there. Until next time!

  • Thank you Myckel, if you need more help elaborate a new question. Hug and good study!

  • 1

    Thank Eden. Just to help the guys, I managed to solve my code in another way. I’m going to post the code...

0

soma = media = cont =  0
maior = menor = 0
r = 's'

while r != 'n':

    while True:

        try:
            n = float(input('Digite um número inteiro: '))
            break

        except ValueError:
            print('Entrada Inválida!')

    soma += n
    cont += 1

    if cont == 1:
        maior = n
        menor = n

    if n > maior:
        maior = n

    elif n < menor:
        menor = n

    r = str(input('Quer continuar? S/N? ')).strip()

    while (r not in 's' and r not in 'n') or r == '':
        r = str(input('Quer continuar? S/N? ')).strip()

    if r not in 's':
        r = 'n'

media = soma / cont

print('\033[36m<=>\033[m'*10)
print('Foram digitados \033[32m{}\033[m números e a \033[32mmédia\033[m entre eles foi de \033[32m{:.2f}\033[m.'.format(cont, media))        
print('O \033[32mmaior\033[m número digitado foi \033[32m{}\033[m.'.format(maior))
print('O \033[32mmenor\033[m número digitado foi \033[32m{}\033[m.'.format(menor))
  • I have programmed on the phone, I’m without computer, it seemed that the code was well edited, I do not know what happened. Programming on cell phone is complicated.That’s bad.. Thanks for editing...

Browser other questions tagged

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