1

Hey Guys I am pretty new to Python and I am learning this programming language. I am using the Python IDE (GUI) for running all my codes. I have covered till the topic of defining custom procedures. However upon execution it is not giving any output.

Here is my code below. I want to define a procedure for adding two numbers then print the result for any two numbers that I enter.

def sum(a,b):
    print "The Sum Program"
    c = sum(10,14)
    print "If a is "+a+" and b is "+b++ then sum of the them is "+c

What do you think I am doing wrong here?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Rahul Ghosh
  • 21
  • 1
  • 1
  • 5

3 Answers3

6

You have created an infinite loop here; within the sum method you call the sum method always.

What you should do is move your print statements outside the sum method. What goes in the sum method is a return statement that returns your sum.

So, your whole program should look like this (EDIT: Added str() calls, thanks @DSM):

# The procedure declaration
def sum(a,b):
    return a+b

# Your output code
print "The Sum Program"
a = 10
b = 14
c = sum(a, b)
print "If a is "+str(a)+" and b is "+str(b)+" then sum of the them is "+str(c)
Cat
  • 66,919
  • 24
  • 133
  • 141
  • The final print statement has several problems: neither `a` nor `b` are defined, and if they were integers you couldn't add them to strings anyway. – DSM Aug 15 '12 at 18:04
  • Yeah I used this code. But my print statements are giving errors. Some syntax errors. – Rahul Ghosh Aug 16 '12 at 05:37
  • That's.. really, really odd. Can you briefly paste the syntax error and indicate the line it points to? – Cat Aug 16 '12 at 05:42
  • @Eric I am attaching an image of my problem. I posted one image but I think it was deleted by moderators. – Rahul Ghosh Aug 16 '12 at 18:27
2

One thing you may want to try is calling your function something else (since sum is a built-in Python function, as you seem to know since you're using it as well :) ). You could do something like this:

def my_sum(a, b):
    return a + b

print 'The Sum Program'
a = 10
b = 14
c = my_sum(a, b)
print ('If a is ' + str(a) + 
       ' and b is ' + str(b) + 
       ' then the sum of them is ' + str(c))

Note the str()'s - this is used to cast the integers as strings so that they may be joined into the overall string. There are some more elegant ways to do this, but one step at a time :)

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • @RahulGhosh Hmm, it seems to work for me. Where are you getting the error? It could be a copy/paste thing but happy to help sort it out. – RocketDonkey Aug 16 '12 at 08:08
  • Attaching an image of the exact problem. – Rahul Ghosh Aug 16 '12 at 18:26
  • I am sorry I do not have 10 reputation points to attach an image. its giving me a syntax error for the print 'The Sum Program' line. – Rahul Ghosh Aug 16 '12 at 20:38
  • Have you verified the indentation is correct in the lines above? Making sure that `return a + b` is two (or four) spaces in? Python needs that whitespace :) I say that because @Eric's solution above works just fine for me as well, so it could just be a simple formatting thing when you paste. – RocketDonkey Aug 16 '12 at 20:40
  • I just copy pasted your code above in Python. The exact name is 'IDLE Python (GUI)' – Rahul Ghosh Aug 17 '12 at 05:25
  • Can you paste the exact traceback that you get when it fails? – RocketDonkey Aug 17 '12 at 05:48
  • Its highlighting the first print that says 'The Sum Program' in red and then at the end of the code it is showing a message in red as well which reads: SyntaxError: invalid syntax – Rahul Ghosh Aug 17 '12 at 16:18
  • Can you post a screenshot to http://imgur.com/? Happy to go into chat too if you'd like. – RocketDonkey Aug 17 '12 at 16:36
  • Can I post my Skype username here. Cause I don't wanna annoy the moderators..so I am asking first. – Rahul Ghosh Aug 17 '12 at 16:43
  • My Skype username is: funfusion999 . Could you please add me and help once...cause I think there is some error with my print function. I have no clue why this is happening with all my codes. – Rahul Ghosh Aug 18 '12 at 20:06
0
def sum(a, b):
   print "The Sum Program"
   c = a + b
   print "If a is " + str(a) + " and b is " + str(b) + " then the sum of them is " + str(c)

# call it somewhere else with parameters:
sum(10, 14)

You should split IO from computations though.

I recommend Wikibooks on Python. But there are several tutorials out there covering the basics and more.

f00id
  • 113
  • 5