[Tutor] generate random number list and search for number

Steven D'Aprano steve at pearwood.info
Tue Aug 11 14:29:52 CEST 2015


Hi Stephanie,

My comments below, interleaved between yours.

On Tue, Aug 11, 2015 at 03:18:59AM +0000, Quiles, Stephanie wrote:

> Here is the thing I am totally lost. Here is what i have so far… i 
> created the main function and i ask the user to search for a number.

You're not totally lost! Only a little bit lost. You've actually done 
really well to get to the point you are now.


> I 
> have assigned a range of 100. but here is one of my questions how do i 
> assign to search within a value range(for example, i want the program 
> to spit out 100 numbers between 1-300).

Remember how the instructions say to write a function createRandnums()? 
You haven't done that, so you need to. It will take two parameters, the 
count of random numbers to return, and the highest value to allow. Since 
it will return a list of numbers, we need to start with an empty list, 
do some stuff to fill the list, then return it:


def createRandnums(count, highest):
    alist = []
    stuff
    return alist


where you replace "stuff" with your actual code.

You should start by grabbing the code from your main() function that 
loops 100 times, and put it in the createRandnums function instead. Each 
time you generate a random number between 1 and highest, instead of 
printing it, you append it to the list:

alist.append(the_number)

Finally, instead of hard-coding that the loop will ALWAYS be 100, you 
want to loop "count" times instead:

# loop 10 times:
for i in range(10): ...

# loop 20 times:
for i in range(20): ...

# loop "count" times, whatever value count happens to be:
for i in range(count): ...

Does that help?



>  how would i go about doing 
> this? also how do i call the orderedSequentialSearch so i can check to 
> see if value is in list?

result = orderedSequentialSearch(the_random_numbers, the_value_to_search_for)
if result:
    print "the number is found"
else:
    print "No number for you!"



Hope this helps!


-- 
Steve


More information about the Tutor mailing list