Why it say that the result is not defined although i have assign some value to result?


#1

def perimeterofsquare(b):
result = 4 * b
print "The %d is %d."% (perimeterofsquare ,result)
perimeterofsquare(2)


#2

Are you asked to create a function and create that variable in the function? If not, then they probably mean that there should be a global variable named result, but you are creating one locally in your function which ceases to exist as soon as your function returns.

Also note that your code as posted will not run at all, perhaps it should instead look like this:

def perimeterofsquare(b):
    result = 4 * b
    print "The %d is %d."% (perimeterofsquare ,result)

perimeterofsquare(2)

BTW, a function like that would normally not print anything, but instead return the result which can then be printed somewhere else or be used by other functions which isn't possible when you print the result.


#3

I have done indentation error in posting but how i would print it .
And i have seen at many places this way of defining something new
For your concern see this example
def square(n):
"""Returns the square of a number."""
squared = n**2

return squared

Call the square function on line 9! Make sure to

include the number 10 between the parentheses.

square(10)


#4

def square(n):
"""Returns the square of a number."""
squared = n**2

return squared

Call the square function on line 9! Make sure to

include the number 10 between the parentheses.

square(10)
I know about it but what to do for result undefined


#5

there is something going wrong with the posting page .Some parts at the bottom always left


#6

use markup to keep the indent of your code intact:

could you make 1 clear reply which contains the following:
- the code you have trouble with (with markup so indent shows)
- the error message
- the exercise url

Then we can help you further, thank you :slight_smile:


#7


#8

I hope this will resolves issue


#9

the print should be nested inside the function (indent it)

and the first %d represent the number, right? perimeterofsquare is the function, b contains the number


#10

Actually the statement is The perimeter of square is result .(perimeterof square , result)
How come first d consist of number . Secondly what i remember that we need to keep print statement away from indentation level thats what i have done here , and why result is undefined ?


#11

well, the function has a parameter (b) which will hold the number, as you can see:

def perimeterofsquare(b):
    print b
perimeterofsquare(2)

perimeterofsquare is the function, b holds the actual value.

result is local variable, it only exist inside the function (variable scope)


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.