How to use strip() next to a list or tuple in python?

Asked

Viewed 953 times

2

I am creating a script and in a snippet I create a list and then use the strip() function next to the upper() inside the v1 input, but when calling the script and executing it does not return anything, how can I solve this?

....
    opcao_sim = ('SIM','YES')
    opcao_nao = ('NÃO','NO')
    v1 = str(input('Deseja criar um repositorio? n[s]: ')).strip().upper()[0]
    if v1 in op_sim:
            print('ok! bons estudos XD')
            sys.exit(0)
    ...

2 answers

1

I understood my own mistake, in case someone is having that same doubt.

  sim = ('S','Y')
    nao = ('N')
    v1 = str(input('Digite sim ou não ')).strip()[0].upper()
    if v1 in sim:
            print('você digitou sim ')
    elif v1 in nao:
            print('Você digitou não ')

inside the list you have to put the letter you want to pick up, it was a very silly mistake.. and if you are going to use the . upper() put the letter in uppercase or if it is . Put() in minuscule

recalling that the [0] means you want the first letter, since the python starts from 0

0

Your code is validating with op_sim, but I believe it is option.

Regardless of this you are taking the first character of the answer ("S", "Y" etc) and looking for it inside the tuple ("SIM", "YES"). The simplest solution is to replace the tuple with a string, something like opcao_sim="SY"and then the validation will work correctly.

Tip, it’s better to use v1=str(input("...")).strip()[0].upper() if you only need the first character of the answer.

Browser other questions tagged

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