These tutorials make me do such basic shit, so I decided to do my own thing and copy some built in functions. Here's my first attempt at a range function. From lurking around here I've learnt that there are many ways to do the same thing, and some ways are more efficient than others. Am I doing this the complete noob way? Is there a more efficient way?
def myrange(a, b, c):
while True:
if b == 0:
print('incorrect b value')
break
else:
i = a
a += b
if abs(a) > abs(c + b):
break
else:
print(i)
continue
print(a)
myrange(1, 1, 10)
#Im pretty proud of my work, considering the time I have been programming, but I want to learn to be better, so any constructive criticism is welcome. If you guys want me to stop posting n00b programs tell me and I will stop.
Name:
Anonymous2009-03-28 0:54
I'm going to be honest and say I was going to critique you and provide a far superior one version but I honestly can't figure out what the fuck that is supposed to be doing, particularly given it is finding something to do with a range?
The program, if and only if |a| > |c| and b!=0, prints a followed by a+b. How in gods name does this relate to a range?
Name:
Anonymous2009-03-28 1:00
>>2
Basically what I'm saying is do you have any documentation for what this function is supposed to do, i.e.: the original problem description?
Name:
Anonymous2009-03-28 1:02
In the tutorial one of the functions they showed us looked something like this:
for x in range(1, 1, 10):
print(x)
*******************
OUTPUT:
1
2
3
4
5
6
7
8
9
*******************
I didnt like the fact that the 10 was not inclusive so I wanted to make my own version of this built in function, so this is what I did. Maybe I am not seeing the bigger picture and that my function wont work the same way as a "for x in range(a, b, c)" loop but thats all i was trying to do.
As for the |a| > |c|, thats the only way i could think of making it stop going printing to infinity. maybe I should have documented that
a is start,
b is step
c is last value (inclusive unlike built in range)
Do you get what I am trying to do?
How would you have done it?
Name:
Anonymous2009-03-28 1:04
>>4
I would have added one to the third argument of the function call.
Name:
Anonymous2009-03-28 1:05
>>4
an example of my function is
myrange(1, 2, 20)
By the way thanks for actually helping me and not just saying "get of mah /prog/ faggot"
Name:
Anonymous2009-03-28 1:10
I don't know why you're using another variable to store a. When (if abs(a) > abs(c+b)) would equal (if abs(a) > abs(c)) after the whole adding thing...
def myrange(a, b, c):
while True:
if b == 0:
print('incorrect b value')
break
else:
if abs(a) < abs(c):
print(a+b)
continue
print(a)
else:
break
Name:
Anonymous2009-03-28 1:16
at first i tried abs(a)> abs(c) but it didnt make my c value inclusive.
i.e myrange(1,1,4) would only output
1
2
3
instead of
1
2
3
4
so i changed it to abs(a) > abs(c+b)
Name:
Anonymous2009-03-28 1:18
>>8
have u tried running that code?
it gives
2
2
2
>>4 I didnt like the fact that the 10 was not inclusive so I wanted to make my own version of this built in function def myrange(a, b, s = 1):
return range(a, b + 1, s)
Name:
Anonymous2009-03-28 10:37
irange = lambda a, b=None, s=1: range(*(0 if b == None else a, a + 1 if b == None else b + 1, s))
Name:
Anonymous2009-03-28 13:57
range() doesn't print anything, it just constructs a list.
range(1, 5, 1) returns [1, 2, 3, 4]
So for your inclusive range (with the last two arguments switched) you can do this
def myrange (a, b, c):
ret = []
while (a <= c):
ret.append(a)
a += b
return ret