0

I have just started to study Python and I am now in the process of doing some exercises.

Specifically, this exercise asks to write a Python program to sum three given integers. However, if two values are equal sum will be zero.

This is my code:

   from sys import argv
   script, x, y, z = argv

   def sum(x, y, z):  
       if x == y or y == z or x==z:  
          sum = 0  
       else:  
          sum = x + y + z  
       return sum  
   print (sum)

I am opening the script in WindowPowerShell specifying the script name (es3.py) and the three variables, e.g: 1 2 3.

However I get the following message:

<function sum at 0x0000000001E0F048>

instead of the result I was expecting.

Does anyone have any ideas of why this is happening?

Thanks.

2
  • Sorry, this is the message I get function sum at 0x0000000001E0F048 Commented Mar 3, 2016 at 14:39
  • What are you expecting? Commented Mar 3, 2016 at 14:40

5 Answers 5

2

Let me comment your code:

script, x, y, z = argv
# we now have four new variables inside the main module: 
# script, x, y, z
# all functions defined here have access to these but
# cannot modify them (unless we use "global").

def sum(x, y, z):
    # while sum() in principle has access to the 
    # variables defined outside of sum(),
    # sum() (the function) also takes three parameters.
    # these need to be passed in with a function call
    # (i.e. sum(x, y, z)).
    #
    # x, y, z here are unrelated to x, y, z outside of sum().
    # in fact the parameters shadow the global variables,
    # making them inaccessible (via direct means).
    if x == y or y == z or x==z:  
        sum = 0  
        # this defines a local variable "sum".
        # it is unrelated to the function "sum"
    else:  
        sum = x + y + z  
        # x, y, z are strings, so "sum" is now a string, too.
    return sum

print (sum)
# here, you refer to the function "sum".
# the variable "sum" only exists inside the function "sum".

If you want the result of sum() (the function), you need call it with parameters:

print(sum(x, y, z))

You cannot access the variable sum outside of the function.

Sign up to request clarification or add additional context in comments.

Comments

0

If you

print(sum)

then you print the function sum.

If you want to print the result, you must call sum:

print(sum(x, y, z))

Comments

0
print (sum(x,y,z))

means call your function/method and print the return value.

print (sum) 

means print the meta data of your data(sum is also a type of data)

Comments

0

You should print the result of the sum function :

print(sum(x, y, z))

This way the return value of the sum function will be printed out. The way you did it, just prints out a function reference.

Also as a side note, that sum() is a built-in python function, so it's best to avoid naming conflicts and name your functions accordingly.

Comments

0

Try the following code:

from sys import argv
script, x, y, z = argv

def sum_(x, y, z):  
    if x == y or y == z or x==z:  
        the_sum = 0  
    else:  
        the_sum = int(x) + int(y) + int(z)  
    return the_sum  
print(sum_(x, y, z))

Comments

Your Answer

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.