3

I'm new to python and I am exploring. Now in the following code, I am trying to call a function from a print statement, is it legal?

# My func
def summer(num1,num2):
print "The summation of the numbers is: %d " % (num1+num2)

num1 = raw_input("Enter a number: ")
num2 = raw_input("Enter another number: ")

print "Again, the summation: %d " % (summer(num1,num2))

print "That is the end of the program."

Now is the line

 print "Again, the summation: %d " % (summer(num1,num2))

valid? I am getting an error.
Can I call a function in this manner, from a string? Thank you.

The error that I get is:

  Traceback (most recent call last):
  File "myfunc.py", line 9, in <module>
  summer(num1,num2)
  File "myfunc.py", line 3, in summer
  print "The summation of the numbers is: %d " % (num1+num2)
  TypeError: %d format: a number is required, not str
2
  • Getting "an error", please be specific and mention the exact error you get. Commented Jan 20, 2017 at 12:36
  • I have edited the error in the question. The answer has already been provided by @Eric Duminil. Commented Jan 20, 2017 at 12:57

3 Answers 3

2

You need to indent the code in your method, and return a String. You also need to convert your strings to integers :

def summer(num1,num2):
    return "The summation of the numbers is: %d " % (int(num1)+int(num2))

num1 = raw_input("Enter a number: ")
num2 = raw_input("Enter another number: ")

print "Again, the summation: %s " % (summer(num1,num2))

print "That is the end of the program."
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, this explains this. Moreover, when I am taking raw-input, what is the default format it stores/takes in the data? string? My question is, what are converting into int in int(num1)
Raw_input always returns a string, even if the content looks like a number. For arithmetic, you need to convert "3" to 3.
1

This is simple. To convert a string to a number do this:

int(String)

after whatever you want to convert to an integer

Therefore, you would do:

num1 = raw_input("Enter a number: ")
num1 = int(num1)

but a faster way would be:

num1 = int(raw_input("Enter a number: "))

That second to last method can be used if you will use num1 as a string and a number (it will change to a number after int(num1)

Sorry just noticed another flaw. (Edited)

I was curious why you had to show the sum before the numbers were defined, and also wanted to tell you that you dont need the %d in the 3rd line and 3rd to last line, just go ahead and do

print "The summation of the numbers is", (num1+num2)

Another Flaw (Edit #2)

The last comment changed your print "The summation of the numbers is: %d " % (num1+num2) to return "The summation of the numbers is: %d " % (num1+num2) but the person did not go over it with you. When you do return, you make the value of the command what the return value is. Let me give you an example.

def MultNumByTwo(num):
    num = num * 2
    return num
print MultNumByTwo(3)

This will print six, because MultNumByTwo takes num which I have made 3 and does num = num * 2 which gives it a new value of itself times two. Now, when I do return num, MultNumByTwo takes on the value of the number after return.

Now lets look at your code. When you do print "The summation of the numbers is: %d " % (num1+num2), the command has no value. It will just do what it was told which was add these two numbers. Therefore, you can only use it alone because it has become a command. You cannot use it in a print command.

If %d were to take commands in a print command anyways, it would be Again, the summation: %d but %d would be The summation of the numbers is: %d " % (num1+num2) and the entire command would print

Again, the summation: The summation of the numbers is: (num1+ num2)

Comments

0

Did small modification to your code. can get int values for num1 and num2 by just using input()

def summer(num1,num2):
    print "The summation of the numbers is: %d " % (num1+num2)
    return num1+num2

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")

print "Again, the summation: %d " % (summer(num1,num2))
print "That is the end of the program."

Output :

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Enter a number: 5
Enter another number: 10
The summation of the numbers is: 15 
Again, the summation: 15 
That is the end of the program.
>>> 

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.