1

I had to create the following code to determine how much radiation one person was exposed to in certain amount of time. I created it with a for loop and my answer was 75% correct, I checked on a friend's code that used a while loop and, he had it 100% correct so, my question is Why or what is the difference between then both codes or what am I not doing in the For loop?

I called the function with these lines

radiationExposure(0, 11, 1)
radiationExposure(40, 100, 1.5)

and this is the code:

def f(x):
    import math
    return 10*math.e**(math.log(0.5)/5.27 * x)

def radiationExposure(start, stop, step):
    cuenta = 0.0
    Iter = stop - start

    for i in range(start,(Iter)+start):
        temp = f(i) * step
        cuenta += temp
    return cuenta

the other code (this is correct):

def f(x):
    import math
    return 10*math.e**(math.log(0.5)/5.27 * x)

def radiationExposure(start, stop, step):      
    result = 0
    while start < stop:
        result += f(start) * step
        start += step
    return result

2 Answers 2

2

You are ignoring the step parameter in your range. The while loop increments by adding step to start, but you increment by 1 only.

You could include the step value in your for loop:

for i in range(start, stop, step):

Note that I eliminated the Iter variable; you don't need it and it is redundant. Just use stop as the end value for the range() object.

Now i will be set to start, start + 1 * step, start + 2 * step, etc. rather than start, start + 1, start + 2, etc.

You probably did this because range() doesn't support floating point values. You cannot step with 1.5, so to solve this question properly you have to use a different kind of loop.

You can still use range() if you really wanted to:

length = int(1 + (stop - 1 - start) / step)
for counter in range(length):
    i = start + counter * step
    temp = f(i) * step
    cuenta += temp

This calculates first how many steps in total your loop will have to perform, then loops that many times. Each iteration the actual value for that iteration is calculated from the loop counter.

I'd say using a while loop is easier.

At least with that change, the results of both approaches is the same:

>>> import math
>>> def f(x):
...     return 10*math.e**(math.log(0.5)/5.27 * x)
... 
>>> def radiationExposure_while(start, stop, step):
...     result = 0
...     while start < stop:
...         result += f(start) * step
...         start += step
...     return result
... 
>>> def radiationExposure_range(start, stop, step):
...     result = 0
...     length = int(1 + (stop - 1 - start) / step)
...     for counter in range(length):
...         i = start + counter * step
...         result += f(i) * step
...     return result
... 
>>> radiationExposure_range(0, 11, 1) == radiationExposure_while(0, 11, 1)
True
>>> radiationExposure_range(40, 100, 1.5) == radiationExposure_while(40, 100, 1.5)
True
Sign up to request clarification or add additional context in comments.

16 Comments

As a random trivia question, any idea why range doesn't support float values for step? Seems like it would be super trivial to account for.
@Cyber: because float arithmetic isn't so simple? It may be for the 1.5 step size case, but what about really tiny step sizes? Or using float('inf') as a start or stop value, etc.? When using float, you need to deal with more corner cases that require explicit handling.
That's true I guess, always have to protect against bad inputs. Seems like a pain in the butt.
I did not used step in the range() does not support floating ... but if I do it the way you give me in the example it goes very far from the answer, please try with this test cases: radiationExposure(0, 5, 1) >>>39.10318784326239 radiationExposure(5, 11, 1) >>>22.94241041057671 radiationExposure(40, 100, 1.5)>>>0.434612356115
@EstebanG.Gutierrez: I just did and the outcome for the while vs. the for .. in range approach I gave in my answer is exactly the same for those inputs.
|
1

Your for loop steps by 1 and ignores the step parameter

for i in range(start, (Iter)+start)

To account for the step, add the last parameter to range

for i in range(start, (Iter)+start, step)

1 Comment

@MartijnPieters You are right, I was double-confused

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.