83.8k views
5 votes
Define and test a function myRange. This function should behave like Python’s standard range function, with the required and optional arguments. Do not use the range function in your implementation! (Hints: Study Python’s help on range to determine the names, positions, and what to do with your function’s parameters. Use a default value of None for the two optional parameters. If these parameters both equal None, then the function has been called with just the stop value. If just the third parameter equals None, 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.)

2 Answers

4 votes

Final answer:

The function myRange mimics Python's standard range function, accepting start, stop, and step parameters, and returning a list of numbers counting up or down. It handles one to three arguments, using default values when necessary, and a test case showcases the expected output.

Step-by-step explanation:

The function myRange is intended to replicate the behavior of Python's standard range function without using the built-in range. This function accepts up to three parameters: start, stop, and step. If only one argument is provided, it is assumed to be stop, with start defaulting to 0 and step to 1. If two arguments are provided, they are treated as start and stop, with step defaulting to 1. If all three arguments are provided, they represent start, stop, and step respectively.

Here is an example of how the function can be defined and used:

def myRange(start, stop=None, step=None):
if stop is None: # One argument case
stop = start
start = 0
if step is None: # Two argument case
step = 1
i = start
result = []
while (step > 0 and i < stop) or (step < 0 and i > stop):
result.append(i)
i += step
return result

# Test cases
print(myRange(10)) # Equivalent to range(10)
print(myRange(1, 10)) # Equivalent to range(1, 10)
print(myRange(1, 10, 2)) # Equivalent to range(1, 10, 2)

Each call to myRange generates a list with numbers starting at start, ending just before stop, and increments by step.

User BrunoRamalho
by
5.6k points
5 votes

Answer:

#section 1

def ran(first, *last):

li = []

#section 2

try:

if len(last) > 2:

raise ValueError

elif len(last) == 1:

while last[0] > first:

li.append(first)

first = first + 1

print(li)

elif len(last) == 2:

while last[0] > first:

li.append(first)

first = first + last[1]

print(li)

else:

i=0

while i < first:

li.append(i)

i= i + 1

print(li)

except:

print("Ran expected at most three arguments at most got", len(last)+1)

Step-by-step explanation:

#section 1

The function is defined and a list is created to hold the range of values specified.

It is important to note that the function was created using one required argument and using by *args (*last), The function is allowed to accept extra positional arguments.

#section 2

In this section, we limit the extra positional arguments to two using a try and except block and raising a value error anytime a positional arguments more than three (remember we have 1 required argument) is entered.

The *args supply their arguments in a tuple.

Finally, within the try and except block depending on the arguments given; IF, ELIF and ELSE blocks are available to take those arguments and return the appropriate answer to generate the required range of values

User Mrunal Thanki
by
5.9k points