First off this is for a homework assignment and I am new to python. I have attempted to tweak an example of how to make a table from another post on this forum but with no luck. Here are my problems:

1) my range is small (0, 1, 0.1)
This is my error:

Warning (from warnings module):
File "C:/Users/Will/Documents/Python Codes/Sin", line 25
for x in range(0., 1., 0.1):
DeprecationWarning: integer argument expected, got float

Traceback (most recent call last):
File "C:/Users/Will/Documents/Python Codes/Sin", line 25, in <module>
for x in range(0., 1., 0.1):
ValueError: range() step argument must not be zero

I appreciate any help. My thoughts are that my range increments (0.1) are returning 0 and I am unsure as to how I can fix that.

``````import math

# I am defining a function for Sin(x)
def function_sin(x):
n = 0.
top = 1.
bottom = 1.
eps = 10**(-8)
total = 0.
#This is my formula to approximate Sin(x)
while abs(top/bottom) > eps:
n = n + 1
top = ((-1.)**(n-1.))*(x**(2.*n-1))
bottom = math.factorial(2.*n-1.)
total = total + top / bottom

# this is where I make the table using the function
print
print " A table of results for varies values of X in the function Sin(x) "

#the range is from 0 to 1 in incriments of 0.1

for x in range(0., 1., 0.1):
print "%3.3f x-value = %3.3f output" % ( x ,function_sin(x) )``````
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by vegaseat

range is only for integers.

You should either use numpy:

``````import numpy
numpy.arange(0,1,0.1)``````

or you can do this: `[x * 0.1 for x in range(0, 10)]` or you can do this:

``````def floatrange(start, stop, step):
while start < stop:
yield start
start += step
print ["%s" % x for x in floatrange(0,1,.1)]``````

Also, you should post problems that need answers as regular forum threads, not code snippets.

Edited by jcao219: n/a

Thank you very much that is exactly what I needed!

On a side note, I am having a large amount of trouble getting numpy and matplotlib to function correctly. I am running windows 64-bit.

Code snippets are for fully functioning code only!!!!
Ask questions in the regular Python forum!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.