When a repetition is made in case there is no answer, no change is made! pq?

Asked

Viewed 21 times

1

It’s a simple code, but based on what I’ve seen and been studying about repetition structure right here, I’ve come up with a way to do it, but I’m dealing with the following problem: the value is not being added after the option given by the program is being performed! always giving me as return the value of 0 (zero)!.

follow:

valor = int(input('Valor Solicitado: '))
valor1 = int(0)

while True:
    parcelax = input('Qual formato de parcelamento?  \n 1) Á Vista \n 2) 12x  \n 3) 30x \n 4) 60x  \n' )
    if parcelax not in {'1','2','3','4'}:
        print('Digito incorreto!')
        continue
    if parcelax == 1:
        valor1 = 20
    break
print('Olha: {}'.format(valor1))
  • 1

    It is worth commenting that 0 is already an entire value, so do int(0) is redundancy. You can do only valor1 = 0.

1 answer

1

Is that the method input returns a string.

You were testing if parcelax == 1: as if parcelax be a whole.

Option 1: Then either you switch to if parcelax == '1': test the value as string.

Option 2: You convert parcelax for the whole in this way if int(parcelax) == 1:

Follow the solution with option 1:

valor = int(input('Valor Solicitado: '))
valor1 = int(0)

while True:
    parcelax = input('Qual formato de parcelamento?\n 1) Á Vista \n 2) 12x  \n 3) 30x \n 4) 60x  \n' )
    if parcelax not in {'1','2','3','4'}:
        print('Digito incorreto!')
        continue
    if parcelax == '1':
        valor1 = 20
    break
print('Olha: {}'.format(valor1))

Browser other questions tagged

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