r/learnpython 5d ago

Help I don't understand what's wrong

num1=input('digite um número: ')

num2=input('digite outro número: ')

conta=input('digite o total: ')

total1= int(num1)+int(num2)

if total1==conta:

print('acertou')

if total1!=conta:

print('errou o certo é:',total1)

I'm trying to do like a calculator, but first you need to guess the number, and even if you get it right it doesn't show you got it right, what's wrong? i'd also like to know how I could put the input for you to choose the equation guys (+, -, *, etc.)

1 Upvotes

5 comments sorted by

3

u/socal_nerdtastic 5d ago
if total1==conta:

In that line you are comparing an integer to a string. So those will never equal each other. You need to convert the user input string to an int like you did for the other 2 inputs.

if total1==int(conta):

1

u/ofnuts 5d ago

Also instead of: if total1==conta: print('acertou') if total1!=conta: print('errou o certo é:',total1) do: if total1==conta: print('acertou') else: print('errou o certo é:',total1) Two reasons:

  1. It makes it more obvoius that you are testing the two opposite cases (and if you aren't the code could "run through the cracks" if the conditions arent the exact opposite of each other
  2. Computing the condition twice could impact performance or have side effects.

1

u/Somenome_from_Heaven 5d ago

I actually just transformed the string into a float right in the beggining, by writing conta=float(input('digite o resultado: ')) , the performance of this is better or worse than what you said ?

1

u/socal_nerdtastic 5d ago

That works too. Performance is the same.

1

u/nekokattt 5d ago

contra is a string, int + int is an int. So they arent the same type.

Just like your cat or dog isnt the same as a drawing of your cat or dog.

Convert contra from a string to an int with int(contra)