Why is my function returning input1 and input2 as strings? I'm trying to create an addition function
Question:
def addition():
int(input1)
int(input2)
mysum = input1 + input2
print(mysum)
input1: str = input("Enter integer #1 n ")
input2: str = input("Enter integer #2 n ")
addition()
I tried to convert the inputs to ints so that I could add them together but it just treats them like regularly doing:
print(text1("1") + text2("1")
result: 11
I also tried to use sum() with a "for numbers in input1 and input2" statement but produced the same result.
Answers:
Change your function to this
def addition():
mysum = int(input1) + int(input2)
print(mysum)
The function should look like this:
def addition(number1, number2):
print(int(number1) + int(number2))
input1 = input("Enter integer #1 n ")
input2 = input("Enter integer #2 n ")
addition(input1, input2)
These lines:
int(input1)
int(input2)
Don’t change the data type of the variables input1 and input2.
To save the change to the data type, you should use:
input1=int(input1)
input2=int(input2)
def addition():
int(input1)
int(input2)
mysum = input1 + input2
print(mysum)
input1: str = input("Enter integer #1 n ")
input2: str = input("Enter integer #2 n ")
addition()
I tried to convert the inputs to ints so that I could add them together but it just treats them like regularly doing:
print(text1("1") + text2("1")
result: 11
I also tried to use sum() with a "for numbers in input1 and input2" statement but produced the same result.
Change your function to this
def addition():
mysum = int(input1) + int(input2)
print(mysum)
The function should look like this:
def addition(number1, number2):
print(int(number1) + int(number2))
input1 = input("Enter integer #1 n ")
input2 = input("Enter integer #2 n ")
addition(input1, input2)
These lines:
int(input1)
int(input2)
Don’t change the data type of the variables input1 and input2.
To save the change to the data type, you should use:
input1=int(input1)
input2=int(input2)