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

Emile van Sebille emile at fenx.com
Fri Jul 20 01:15:34 CEST 2012


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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>





More information about the Tutor mailing list