Question
I have this question mostly figured out, but I am having trouble with one part. The question is as follows: Define and test a functionmyRange.
I have this question mostly figured out, but I am having trouble with one part. The question is as follows:
Define and test a functionmyRange. This function should behave like Python's standardrangefunction, with the required and optional arguments, but it should return a list. Do not use therangefunction in your implementation!
(Hints: Study Python's help onrangeto determine the names, positions, and what to do with your function's parameters. Use a default value ofNonefor the two optional parameters. If these parameters both equalNone, then the function has been called with just the stop value. If just the third parameter equalsNone, then the function has been called with a start value as well. Thus, the first part of the function's code establishes what the values of the parameters are or should be. The rest of the code uses those values to build a list by counting up or down.)
An example line from the program and the associated output is shown below:
print(myRange(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The code I have is:
defmyRange(start,stop=None,step=None):
lst=[]
if(stop==None):
stop=start
start=0
if(step==None):
step=1
while(start lst.append(start) start=start+step returnlst print(myRange(10)) print(myRange(1,10,2)) print(myRange(10,1,-1)) print(myRange(4,12,2)) What I am having trouble with is the -1 in the third print statement. The outputs for all of the ranges are correct but I can not get the one for the 10,1,-1 to print correctly, it is supposed to count backwards like this: Please help! Thank you!(10, 1, -1) == [10, 9, 8, 7, 6, 5, 4, 3, 2])
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started