coder123
coder123

Reputation: 1

Error with the function variable in my calculator program

I'm new to python and made a calculator program as practice. I have experience in java so I could take off easily but I am still not fluent with variable declaration and functions even though I was a java programmer. So, if you could help me solve this error, it will be much appreciated.

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
oper = str(input("Enter the operator: "))
def my_func(answer):
    print("The answer is " + answer)

if oper == "+":
    calc = num1 + num2
    my_func(calc)
if oper == "-":
    calc = num1 + num2
    my_func(calc)
if oper == "/":
    calc = num1 + num2
    my_func(calc)
if oper == "*":
    calc = num1 + num2
    my_func(calc)
else:
    print("You have not entered a valid operator")

I am getting the following errors when I run this code.

Traceback (most recent call last): File "(file location)", line 13, in my_func(calc) File "(file location)", line 6, in my_func print("The answer is " + answer) TypeError: can only concatenate str (not "int") to str

Upvotes: 0

Views: 122

Answers (5)

C1rb
C1rb

Reputation: 1

There are two problems in the code you have

  1. It produces an error in line 6 which is a TypeError, don't worry it can be solved easily just by doing this Instead of concatenating them you must add them which is done by just putting the str function before the (answer) and making sure that they are strings
    def my_func(answer):
    print("The answer is " + str(answer))
  1. It has wrong operator assignment which can be solved like this:
    if oper == "+":
       calc = num1 + num2
       my_func(calc)
    if oper == "-":
       calc = num1 - num2
       my_func(calc)
    if oper == "/":
       calc = num1 / num2
       my_func(calc)
    if oper == "*":
       calc = num1 * num2
       my_func(calc)

Upvotes: 0

Shallow Frank
Shallow Frank

Reputation: 5

Since java.Obejct has method toString() which you can overwrite:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

When trying to add String with a none String object as follows:

System.out.println("Answer is " + (Object) m);

Java will call m.toString() to convert this operation from "String + none String" to "String + String" so the code executed smoothly.
However python has no similar mechanism, so we need manually spec none str to convert, and one way is:

"The answer is" + str(answer)

Upvotes: 0

xio
xio

Reputation: 640

There are several ways:

convert the int answer to a string

print("The answer is " + str(answer))

Use %-formatting

The % operator (modulo) can also be used for string formatting. Given 'string' % values, instances of % in string are replaced with zero or more elements of values.

print("The answer is %d" % answer)

Use str.format()

The brackets and characters within them (called format fields) are replaced with the objects passed into the str.format() method. A number in the brackets can be used to refer to the position of the object passed into the str.format() method.

print("The answer is {}".format(answer))

Use f-Strings

A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

print(f"The answer is {answer}")

Upvotes: 1

enggPS
enggPS

Reputation: 712

Here, your answer will be in integer format. Therefore, its invalid to concatenate(+) a string and an integer. So, you should modify your code as follows:

def my_func(answer):
    print("The answer is ", answer)

Also, I would like to suggest that instead of multiple if(s) you should have used elif. Otherwise else part will be printed for every case except the last one. So your code should be modified as:

if oper == "+":
    calc = num1 + num2
    my_func(calc)
elif oper == "-":
    calc = num1 - num2
    my_func(calc)
elif oper == "/":
    calc = num1 / num2
    my_func(calc)
elif oper == "*":
    calc = num1 * num2
    my_func(calc)
else:
    print("You have not entered a valid operator")

Upvotes: 0

pigeonburger
pigeonburger

Reputation: 736

You must convert the int answer to a string. str() does this:

def my_func(answer):
    print("The answer is " + str(answer))

Upvotes: 2

Related Questions