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)
-
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...Daren Thomas– Daren Thomas2013年09月05日 12:37:50 +00:00Commented Sep 5, 2013 at 12:37
-
@bur - why the tag change?Lix– Lix2013年09月05日 12:38:30 +00:00Commented Sep 5, 2013 at 12:38
-
3@Lix, because in python-2.x, input actually evaluates the input, so the example given would work!Daren Thomas– Daren Thomas2013年09月05日 12:40:09 +00:00Commented Sep 5, 2013 at 12:40
9 Answers 9
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)
2 Comments
input() return number, ie >>> y = input(), 3, >>> y+3, 6input 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)
2 Comments
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.you can use this :
y=int(input())
Comments
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.
Comments
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
Instead of input(), use int(input()). This will tell Python that the user is about to enter an integer.
Comments
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()
Comments
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)
Comments
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