2

I made a calculator in python but when I run it and do for example 123 and 321 I get 123321 instead of 444, What am I doing wrong?

import time
print("Calculator 1.0")
print("made by AnAwesomeMiner")
print("Number 1 in calculation")
x = input()
print("Number 2")
y = input()
print("calculating")
time.sleep(3)
print("why is this not done yet")
time.sleep(3)
print("god this is taking forever")
time.sleep(3)
print("done")
answear = x + y
print(answear)
Luuklag
3,91412 gold badges40 silver badges59 bronze badges
asked Sep 5, 2013 at 12:31
3
  • 2
    +1 for reminding me of my first programs from when I was eight years old. Wow. I think mine was called MaxMath and I spent most of the time printing a splash screen in Rexx... Commented Sep 5, 2013 at 12:37
  • @bur - why the tag change? Commented Sep 5, 2013 at 12:38
  • 3
    @Lix, because in python-2.x, input actually evaluates the input, so the example given would work! Commented Sep 5, 2013 at 12:40

9 Answers 9

7

input() returns string not number . That's why instead of addition , String concatenation is performed.

you need to use int(x) and int(y) for conversion.

use this statement answear = int(x) + int(y)

answered Sep 5, 2013 at 12:34
Sign up to request clarification or add additional context in comments.

2 Comments

input() return number, ie >>> y = input(), 3, >>> y+3, 6
@suhail only in python 2, in python 3 it returns a string.
6

input returns a string, and when you combine two strings the result is what you are seeing.

>>> x = '123'
>>> y = '321'
>>> x+y
'123321'

So you need to convert them to an integer, like this:

answear = int(x) + int(y)
answered Sep 5, 2013 at 12:36

2 Comments

Oh i thought phyton did that by it self, is there anyway to automate this?
Its quite automatic right now; you have to know what is supposed to be entered. Everything from input is a string, so Python cannot know if you want a number, a float, a file name, or an actual string. So its left up to you to check what the user enters and then do whatever is required of the inputs.
2

you can use this :

y=int(input())
answered Sep 5, 2013 at 12:40

Comments

1

This is because you are declaring it as a string. Use a = int(input()). This will cast it into an integer. If you want to insert a decimal number use the float data type.

buddemat
5,32816 gold badges37 silver badges61 bronze badges
answered Aug 25, 2021 at 9:07

Comments

0

input() accepts and returns a string object and you need to typecast this into an integer (or float) if you want to perform arithmetic operations on it. Performing a + operation on two strings merely concatenates them.

Comments

0

Instead of input(), use int(input()). This will tell Python that the user is about to enter an integer.

answered Aug 4, 2017 at 9:36

Comments

0
def main():
 def add(x,y):
 return x + y
 def sub(x,y):
 return x - y
 def mult(x,y):
 return x * y
 def div(x,y):
 return x / y
 def remainder(x,y):
 return x % y
 repeat=True
 while repeat:
 select=int(input("please select any operation:-\n 1.ADD\n2.SUBTRACT\n3.MULTIPLY\n4.DIVIDE\n5.REMAINDER\nselect here:-"))
 num1=int(input("Enter the first number"))
 num2=int(input("Enter the second number"))
 if select==1:
 print(num1,"+",num2,"=",add(num1,num2))
 elif select==2:
 print(num1,"-",num2,"=",sub(num1,num2))
 elif select==3:
 print(num1,"*",num2,"=",mult(num1,num2))
 elif select==4:
 print(num1,"/",num2,"=",div(num1,num2))
 elif select==5:
 print(num1,"%",num2,"=",remainder(num1,num2))
 else:
 print("invalid input")
 print("Do you want to calculate further?\n press y for continue.\n press any other key to terminate.")
 repeat="y" in str(input())
 if repeat=="y":
 print("Ooo yeh! you want to continue")
 else:
 print("Tnakyou")
main()
answered May 6, 2020 at 3:42

Comments

0

This is a simple problem to fix. When adding to integers or doing any other operation including an input and an int you need to do this:

y = int(input())
x = int(input())
a = y+x

so this put into your code looks like this:

import time
print("Calculator 1.0")
print("made by AnAwesomeMiner")
print("Number 1 in calculation")
x = int(input())
print("Number 2")
y = int(input())
print("calculating")
time.sleep(3)
print("why is this not done yet")
time.sleep(3)
print("god this is taking forever")
time.sleep(3)
print("done")
answear = x + y
print(answear)
answered Jun 16, 2020 at 4:06

Comments

0
 def run_calculation(user_choice):
 num1, num2 = ........
 match : user_choice
 case '1':
 result = addition(num1, num2)
 case '2':
 result = substraction(num1, num2)
 case '3':
 result = multiplication(num1, num2)
 case '4':
 result = division(num1, num2)
 
........ :
 print("Choix invalide.")
 return result 
answered Aug 18, 2023 at 11:58

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.