Please help...

A forum for general discussion of the Python programming language.

Please help...

Postby alabview » Mon Oct 28, 2013 1:11 pm

Hi there,

I have two probems here,

1. I would like to have display i.e. 0.0,0.01,0.02,0.03.........But I can't make it, I have no idea why?
2. If it is possible to do For loop start from -0.01 until 5? IF yes, how to implement it?

For Q1,
I have attached the code and Output

Code: Select all
import ctypes
DLLHANDLE=ctypes.cdll.LoadLibrary("C:\\INSTRDLL\\builds\\DOUDLL\\FORDOU.DLL")
STEPSIZE=0.1
c_STEPSIZE=ctypes.c_double(STEPSIZE)
c_STEPSIZEOUT=ctypes.c_double()
for i in range(5*100):
    c_STEPSIZE=ctypes.c_double(i/100.)
    SOURCEHANDLE=DLLHANDLE.DOUDLL(c_STEPSIZE,c_STEPSIZEOUT)
    print(c_STEPSIZE)
    print(c_STEPSIZEOUT)


Output display as 0.0. I have no idea why..
Code: Select all
c_double(0.0)
c_double(0.0)
c_double(0.01)
c_double(0.0)
c_double(0.02)
c_double(0.0)
c_double(0.03)
c_double(0.0)
c_double(0.04)
c_double(0.0)
c_double(0.05)
c_double(0.0)
c_double(0.06)
c_double(0.0)
c_double(0.07)
c_double(0.0)
c_double(0.08)
c_double(0.0)
c_double(0.09)
c_double(0.0)
c_double(0.1)
c_double(0.0)
c_double(0.11)
c_double(0.0)
c_double(0.12)
c_double(0.0)
c_double(0.13)
c_double(0.0)
c_double(0.14)
c_double(0.0)
c_double(0.15)
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Please help...

Postby tnknepp » Mon Oct 28, 2013 4:01 pm

Python isn't as nice as, say, MatLab when implementing non-integer ranges. You have a few work arounds:

Possibly the best:
Code: Select all
import numpy as np
for r in np.linspace(0,5,5/0.10 + 1):
    print i


You could implement a while loop:
Code: Select all
r = 0
while r <= 5:
    blah
    blah
    blah
    r+=1


You could also implement your own function. Here is one I found on stackoverflow

Code: Select all
def xfrange(start, stop, step):
    while start < stop:
        yield start
        start += step

for r in xfrange(0,5,0.1):
    print r
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 134
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Discussions

Who is online

Users browsing this forum: Google Feedfetcher and 4 guests