[Tutor] range function and floats?

Alan Gauld alan.gauld at btinternet.com
Wed Jan 5 10:26:53 CET 2011


"Joel Knoll" <mindarson at live.com> wrote

> I'm trying to write a simple program to give the 
> sine of each of a range of numbers, including multiples of pi.  
>
> I keep getting a syntax error, highlighting my use of 2pi 
> as an argument in the range, 

range requires integers. You need to scale your floats 
appropriately and convert to integers. Or in your case
use the multipliers of pi as the arguments to range() 
and then multiply by pi when getting the sine.

> ...this is more about learning how the range function and 
> floats work than about writing a super-efficient program.  

Unfortunately they don't work together.

range(0.1,0.5,0.1)  -> [0.1,0.2,0.3,0.4]  doesn't work
you need to do:

for n in range(1,5):  use( n/10 )

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/






More information about the Tutor mailing list