user2924008
user2924008

Reputation:

Python Lists.What am I missing?

from random import *
def f(one):
    x=randrange(0,len(one))
    y=randrange(0,len(one))
    return(one)
print(one(["20","40","60"]))

the function is supposed to take an input parameter a list of strings and generates two random numbers between 0 and the length of the list but how do you return the list with the slice between the two numbers removed.

Upvotes: 1

Views: 65

Answers (2)

jwodder
jwodder

Reputation: 57630

The sublist of a list one from index i up to (but not including!) index j is written in Python as one[i:j]. I think you can figure out the rest from here.

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34176

First you are not calling the function you've created, you must:

print( f(["20","40","60"]) )

Second, your function f() is not doing anything, is just creating two variables x and y but returning the same list it received as parameter.

And to return the sliced list:

from random import *
def f(one):
    x=randrange(0,len(one))
    y=randrange(0,len(one))
    print (x, y)
    return one[x:y]
print(f(["20","40","60"]))

Remember that randrange(x, y) returns a value between x and y - 1

Upvotes: 2

Related Questions