Hello All!! I have an assignement that I am working on and for some reason I cannot for the life of me recall how to do something so simple lol.

The directions are too "Write a function named Exam, which takes one argument n where n>2. Your function creates a list (list1) of n random integers in range (1,5) and print all elements of list1 on one line.

So far what I have is :

def Exam(n):
    import random
    random.randrange(1,5)

I am not sure where to insert the n>2 and even really what it means. If anyone can assist or give hint. It would be appreciated!!

Thanks

Add on!! Okay now I have this code:

def Test(n):
    import random
    random.randrange(1,5)
    list1*[]
    if n>2:
        for i in range (n):
            list1.append(random.randrange(1.5))

Does this seem on the right path?

Edited 4 Years Ago by Delightfully: Adding more code

All you got to do now is to print the list

print list1

or if you want just the numbers on the same line

for i in list1:
    print i,

Here, I took the liberty of re-writing this function, with some validations:

import random
def Exam(n):
    try:
        n=int(n)
        if (n<2):
            print "Invalid argument."
            return
        l=[]
        for i in range (n-1):
            l.append(random.randrange(1, 5))
        print "List: ", l, "\nElements: "
        for i in l:
            print i,
    except ValueError:
        print "It's not a number."

Edited 3 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.