[Tutor] Calling a function does not return what I want it to return
Alan Gauld
alan.gauld at btinternet.com
Fri Jul 20 02:04:23 CEST 2012
On 20/07/12 00:17, Prasad, Ramit wrote:
>
>> def num_diag(var):
>> ans = 0
>> if var <= 3:
>> print("No diagonals.")
>> else:
>> for i in range(num_sides - 3):
>> ans = ans + i
>>
>> return (((var - 3)*2) + ans)
>>
>> num_sides = (int(raw_input("Enter sides: ")))
>> num_diag(num_sides)
> NameError: global name 'num_sides' is not defined
>
> `for i in range(num_sides - 3):`
> Change num_sides to var.
It should work without, because it will pick up the
global variable definition.
It's probably not working the way it was intended to,
but it should work... But changing it to use the
argument would definitely be better.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list