raiprakashfca

Facing problem with functions

Recommended Posts

I wrote this piece of code for practice. I don't see what's wrong in it. Please help.
def add(num1, num2):
    """This function adds the value of arguments given to it. Only works with two arguments"""
    return num1 + num2
def sub(num1, num2):
    """This function substracts the value of arguments given to it"""
    return num2 - num1
def mul(num1, num2):
    """ This function multiplies the value of arguments given to it"""
    return num1 * num2
def div(num1, num2):
    """ This function divides the value of arguments given to it"""
    return num2 / num1
def main():
    num1 = input("What is num1?")
    num2 = input("What is num2?")
    my_result1 = add(num1, num2)
    my_result2 = sub(num1, num2)
    my_result3 = mul(num1, num2)
    my_result4 = div(num1, num2)
    print(my_result1)
    print(my_result2)
    print(my_result3)
    print(my_result4)

main()

    

 

This is the output that I am getting in debugger window - 

 

C:\Python35\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 63680 --file C:/Users/Dell/PycharmProjects/main.py/firststep.py
pydev debugger: process 2028 is connecting

Connected to pydev debugger (build 172.3317.103)
What is num1?5
What is num2?10
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Dell/PycharmProjects/main.py/firststep.py", line 25, in <module>
    main()
  File "C:/Users/Dell/PycharmProjects/main.py/firststep.py", line 17, in main
    my_result2 = sub(num1, num2)
  File "C:/Users/Dell/PycharmProjects/main.py/firststep.py", line 6, in sub
    return num2 - num1
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Process finished with exit code 1

Share this post


Link to post
Share on other sites

The input function ALWAYS returns a string. You are trying to do math on a string which is only allowed sometimes (you can see me discover something strange in one of the videos).

You need to call the int function to convert from a string to an integer (parse). 

Share this post


Link to post
Share on other sites
On 9/6/2017 at 7:11 AM, Kevin said:

The input function ALWAYS returns a string. You are trying to do math on a string which is only allowed sometimes (you can see me discover something strange in one of the videos).

You need to call the int function to convert from a string to an integer (parse). 

Wow! Thanks a lot. Excited to hear from you. 

How can I convert this string to int? exactly at what stage should I do it?

 

Share this post


Link to post
Share on other sites
def add(num1, num2):
    """This function adds the value of arguments given to it. Only works with two arguments"""
    return num1 + num2
def sub(num1, num2):
    """This function substracts the value of arguments given to it"""
    return num2 - num1
def mul(num1, num2):
    """ This function multiplies the value of arguments given to it"""
    return num1 * num2
def div(num1, num2):
    """ This function divides the value of arguments given to it"""
    return num2 / num1

def main():
    num1 = int(input("What is num1?"))
    num2 = int(input("What is num2?"))
    my_result1 = add(num1, num2)
    my_result2 = sub(num1, num2)
    my_result3 = mul(num1, num2)
    my_result4 = div(num1, num2)
    print(my_result1)
    print(my_result2)
    print(my_result3)
    print(my_result4)

main()

Edited my code. But still getting this error.







 

C:\Python35\python.exe C:/Users/Dell/PycharmProjects/main.py/firststep.py
What is num1?5
What is num2?5
Traceback (most recent call last):
  File "C:/Users/Dell/PycharmProjects/main.py/firststep.py", line 25, in <module>
    main()
  File "C:/Users/Dell/PycharmProjects/main.py/firststep.py", line 17, in main
    my_result2 = sub(num1, num2)
  File "C:/Users/Dell/PycharmProjects/main.py/firststep.py", line 6, in sub
    return num2 - num1
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Process finished with exit code 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...