-4

I wanted to dive into Python and I came to a problem when I wanted to make a very primitive calculator.

It asks you to type 2 numbers, and then it would display informations like number1 + number 2 etc.

I was at the point where I was typing in to display a short text and then the amount of the two numbers like print("amount:" number1 + number2) and that one wasn't right, it was just putting the two numbers next to each other, not what they equal.

I don't know how to display it somehow like this (for example):

The two number equals: 100
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Matee
  • 1
  • Not only would a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) help us figure out what's going wrong, but it would also help us tell *you* what's wrong. – jjramsey Sep 15 '21 at 19:38
  • 1
    Have a look at [How can I read inputs as numbers?](https://stackoverflow.com/q/20449427). At the moment you are adding two strings together instead of two numbers, so it doesn't work as you're expecting it to. – Henry Sep 15 '21 at 19:41
  • so i was writing an answer but the question got closed so here's my comment. you need to convert datatypes to same type in order to print. a small example below ===================================================== a = int(input('enter first number')) b = int(input('enter second number')) c = a+b d = a-b e = a*b f = a/b oper = input('enter operator') if (oper == '+'): print ('sum of a and b is', c) if (oper == '-'): print ('subtract of a and b is', d) if (oper == '*'): print ('multiple of a and b is', e) if (oper == '/'): print ('devision of a and b is', f) – Shamvil Kazmi Sep 15 '21 at 20:07

3 Answers3

1

You probably did:

a = input('number 1')
b = input('number 2')
print(a + b)

Here a and b will be str (strings are words or sentences). So if you do + it'll be like:

a = 'a_word'
b = 'another_word'
print('a_word'+'another_word')
>>> a_wordanother_word

What you want is to make the word into a number. You can do this by 'casting it' to an integer (a whole number) or a float (a number with a decimal):

a = int(input('number_1'))  # example: a = 5
b = float(input('number_2'))    # example: b = 1.2345
print(a + b)
>>> 6.2345
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Nathan
  • 3,558
  • 1
  • 18
  • 38
  • You need a medal for deciphering! – Cl0ud-l3ss Sep 15 '21 at 19:51
  • Thank you, now i understand how to have them as int or any other type of variable :) But still another question in my head is how can i print a string and then next to it number1 + number2 so it says "amount:" and then what the two number equals to – Matee Sep 15 '21 at 20:01
  • @Matee `f"amount: {number1 + number2}"` google string formatting – Nathan Sep 16 '21 at 13:13
  • @mkieger1 what was the point of that edit? Some custom I'm not used to? – Nathan Sep 16 '21 at 13:14
0

Instead of number1 = input(), you have to make it number1 = int(input()) (you can also use float() or whatever) , otherwise your number is going to be a string.

  • Thank you, now i understand how to have them as int or any other type of variable :) But still another question in my head is how can i print a string and then next to it number1 + number2 so it says "amount:" and then what the two number equals to – Matee Sep 15 '21 at 20:01
  • You just have to print("Amount:",number1+number2) – RamenNoodle93 Sep 15 '21 at 20:06
-1

I'm not sure of what you are trying to do but i think it should work for your case :

a = 10

b = 5

print(f'A + B = {a+b}')