Re: [Tutor] Calling a function does not return what I want it to return

2012-07-19 Thread Alexander Q.
On Thu, Jul 19, 2012 at 4:21 PM, Dave Angel  wrote:

> On 07/19/2012 06:58 PM, Alexander Q. wrote:
> > I have this little program that is supposed to calculate how many
> diagonals
> > a polygon of x sides has, but it does not return what I have in the
> > "return" part of the function when I call it. Here is the code:
> >
> > 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)
> >
> >
> > Any suggestions as to what is going on? When I run it, it prompts me for
> > the number of sides, and that's it.
> > Thanks.
> >
> >
>
> You never use the return value.  Try assigning it, and printing it.
>
> result = num_diag(num_sides)
> print("final answer=", result)
>
> --
>
> DaveA
>
> That did it- thanks Dave!

-Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling a function does not return what I want it to return

2012-07-19 Thread Alan Gauld

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/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling a function does not return what I want it to return

2012-07-19 Thread Dave Angel
On 07/19/2012 06:58 PM, Alexander Q. wrote:
> I have this little program that is supposed to calculate how many diagonals
> a polygon of x sides has, but it does not return what I have in the
> "return" part of the function when I call it. Here is the code:
>
> 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)
>
>
> Any suggestions as to what is going on? When I run it, it prompts me for
> the number of sides, and that's it.
> Thanks.
>
>

You never use the return value.  Try assigning it, and printing it.

result = num_diag(num_sides)
print("final answer=", result)

-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling a function does not return what I want it to return

2012-07-19 Thread Emile van Sebille

On 7/19/2012 3:58 PM Alexander Q. said...

I have this little program that is supposed to calculate how many
diagonals a polygon of x sides has, but it does not return what I have
in the "return" part of the function when I call it. Here is the code:

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: ")))



You're almost there.  Change the following


num_diag(num_sides)


to

print num_diag(num_sides)
(for pythons < v3)   or

print (num_diag(num_sides))
(for python v3 >)

Then see where that takes you.


Emile




Any suggestions as to what is going on? When I run it, it prompts me for
the number of sides, and that's it.
Thanks.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling a function does not return what I want it to return

2012-07-19 Thread Prasad, Ramit
> I have this little program that is supposed to calculate how many diagonals a
> polygon of x sides has, but it does not return what I have in the "return"
> part of the function when I call it. Here is the code:
> 
> 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)
>


>>> num_diag(5)
NameError: global name 'num_sides' is not defined


`for i in range(num_sides - 3):`
Change num_sides to var.

Ramit

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Calling a function does not return what I want it to return

2012-07-19 Thread Alexander Q.
I have this little program that is supposed to calculate how many diagonals
a polygon of x sides has, but it does not return what I have in the
"return" part of the function when I call it. Here is the code:

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)


Any suggestions as to what is going on? When I run it, it prompts me for
the number of sides, and that's it.
Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor