I can’t get the None result out of this code

Asked

Viewed 73 times

1

I’m trying to pull this one out None, I’ve already put else, I’ve already put add2 and then I defined add = add2 but I haven’t been able to get it out.

How does it look:

Which fruit do you want to add to the list? strawberry

Want to add more fruit? maybe

Please type yes or no:

None (and the text box to type)

(same problem in both ifs)

frutas = []
novafruta = input('Qual fruta você quer adicionar à lista? ')
frutas.append(novafruta)
add = input('Quer adicionar mais fruta? ')

if add != 'sim' or add != 'não':
    add = input(print('Por favor *digite sim ou não: '))
    
while add == 'sim':
    frutas.append(input('Qual fruta você quer adicionar à lista? '))
    add = input('Quer adicionar mais fruta? ')
    if add != 'sim' or add != 'não':
        add = input(print('Por favor digite sim ou não: '))

1 answer

2


Logic is very complicated and redundant. I would even change a little the way of responding to the user to simplify the code, but I don’t know if you can so I’ll keep it that way.

The biggest reason for the mistake is that you have a input() without the need to encapsulate the print(). This makes no sense and makes logic flawed because one only has a chance to make a mistake, then problems. So you have to take that out, but you have to fix all the rest of the logic.

frutas = []
add = 'sim'
while True:
    if add == 'sim':
        frutas.append(input('Qual fruta você quer adicionar à lista? '))
    add = input('Quer adicionar mais fruta? ')
    if add == 'não':
        break
    if add != 'sim' and add != 'não':
        add = print('Por favor digite sim ou não: ')
print(frutas)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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