2

I would like to write a function for range where you enter a value when you are calling the function.

def randomRange():
    for num in range(0, ())
        print num

I would like the user to call the function like this "randomizeRange()"

And then I would like them to enter an integer to represent the maximum value in the range. I'm trying to get a better understanding of how to use the range function.

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
JenTen10
  • 123
  • 1
  • 2
  • 9
  • Also useful for understanding the range function are docs and tutorials, check out https://docs.python.org/3/library/functions.html#func-range , http://pythoncentral.io/pythons-range-function-explained/, etc. Google 'python range' – George Pantazes Sep 28 '17 at 15:13

1 Answers1

3

You need to prompt the user for input using the input() or raw_input() methods and then convert this to an int()

Python 2:

def randomRange():
    for it in range(int(raw_input("How many loops?: "))):
        print it

Python 3:

def randomRange():
    for it in range(int(input("How many loops?: "))):
        print(it)

Example >input and >>output:

>>How many loops?:
> 2
>> 0
>> 1
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65