-2

Right now I'm trying to write a program that takes 20 numbers, and can do a variety of tasks with those numbers, such as finding the smallest/largest integer, find the total, average, etc.

I have the core program working, but I'm trying to add an additional option that allows the user to select a range of numbers, and create a list of 20 values. As far as I know, I also have this written correctly.

The primary issue that I'm having, is that when I try to return the list so that it may be stored within the previously defined list, it won't store any of the values.

Here is the function that I wrote for a user to input their own 20 values:

#Get Number List
def input_numbers():
    print("Please enter a series of 20 numbers, pressing enter after each number.")
    numList=[int(input('Enter a number: ')) for i in range(20)]
    return numList

This works like a charm, no questions asked.

On the other hand, this is my randomly generated list:

#Randomly generate 20 numbers.
import random
def randomly_generate():
    number1= int(input("Please enter the lower number: "))
    number2= int(input("Please enter the higher number: "))
    numList=random.sample(range(number1, number2), 20)
    print(numList)
    return numList

To clarify: I am only printing within the function to see if the random number generator works, it is. It will print 20 values within the specified range.

However, when I return numList, it is not storing properly like the first function is. This causes issues for me, because I cannot perform my other functions with an empty list.

EDIT: Here is what caused the issue: I was returning the value, but not redefining numList, thus creating my issue.

        elif choice == RANDOMLY_GENERATE:
            randomly_generate()

#Randomly generate 20 numbers
#Import Random
import random
def randomly_generate():
    number1= int(input("Please enter the lower number: "))
    number2= int(input("Please enter the higher number: "))
    numList=random.sample(range(number1, number2), 20)
    print(numList)
    return numList
  • 1
    It is unsafe to use `int(input(x))` as it is the same as `int(eval(raw_input(x)))` instead use `int(raw_input(x))`. – Dan D. Mar 02 '15 at 21:28
  • 1
    Where are you trying to store the results? How do you know it's not storing them correctly? – Peter Wood Mar 02 '15 at 21:32
  • 3
    @DanD. not under Python 3, where [`raw_input` is now called `input`](http://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3-1) – zehnpaard Mar 02 '15 at 21:35
  • 1
    Doesn't return an empty list for me. How about editing the code that calls this function into your question? – Dan Getz Mar 02 '15 at 21:39
  • @BrianMorris You don't assign the return value to anything. – Peter Wood Mar 02 '15 at 21:41
  • @DanGetz I'm just calling the function in the same way that I am calling the first function on my post. I will take a screenshot of my code. [Screenshot](http://puu.sh/gjMMf/a0eea70895.py) – Brian Morris Mar 02 '15 at 21:44
  • 1
    If you look at your code, that's clearly not true. You're clearly calling the first function in one way, and the others differently — the first one, you use the return value. The others, you ignore it. – Dan Getz Mar 02 '15 at 21:46

3 Answers3

1

Here's a simple example:

import random

def randomly_generate():
    number1= int(input("Please enter the lower number: "))
    number2= int(input("Please enter the higher number: "))
    numList=random.sample(range(number1, number2), 20)
    print(numList)
    return numList

if __name__ == "__main__":
    returned_list = randomly_generate()

    print(returned_list)

Looking at your code:

#Randomly generate 20 numbers
elif choice == RANDOMLY_GENERATE:
    randomly_generate()

You can see that you're simply calling the function but not assigning the return of the function to any variable. You'll need to do something like this:

r_list = []
#...

#Randomly generate 20 numbers
elif choice == RANDOMLY_GENERATE:
    r_list = randomly_generate()
James Mertz
  • 8,459
  • 11
  • 60
  • 87
  • Thank you! This fixed my issue! I appreciate the advice. – Brian Morris Mar 02 '15 at 22:00
  • I didn't realize that I didn't set numList = randomly_generate() under my elif statement. I did this for the user input values, but not the randomly generate values. I feel bad for wasting some of your time, but it helped me astronomically. – Brian Morris Mar 02 '15 at 22:10
  • 1
    @BrianMorris we all make mistakes like this at some point or another. Glad I could be your [rubber ducky](http://en.wikipedia.org/wiki/Rubber_duck_debugging). – James Mertz Mar 02 '15 at 22:18
0

random.sample(sample, k) expects the sample size to be of size k. If you run your second function with input 0, and 20 (elements 0..19) it will work fine. What you want to use is the function random.randrange(sample)

https://docs.python.org/2/library/random.html#random.randrange

#Randomly generate 20 numbers.
import random
def randomly_generate():
    number1= int(input("Please enter the lower number: "))
    number2= int(input("Please enter the higher number: "))
    numList= [ random.randrange(number1, number2) for i in range(20) ]
    print(numList)
    return numList
ProfOak
  • 541
  • 4
  • 10
  • `[ random.choice(range(number1, number2)) for i in range(20) ]` is essentially the same as `random.sample(range(number1, number2), 20)` – James Mertz Mar 02 '15 at 21:46
  • `>>> import random as r` `>>> r.sample(range(1,5), 20)` `Traceback (most recent call last):` `File "", line 1, in ` `File "/usr/lib/python3.4/random.py", line 315, in sample` `raise ValueError("Sample larger than population")` `ValueError: Sample larger than population` – ProfOak Mar 02 '15 at 21:51
  • My assumption is that the user knows it should be a range greater than 20. Besides that's not the core issue here. – James Mertz Mar 02 '15 at 21:53
0

I also suggest using a xrange() iterator, to call the random.sample; in the documentation it is clearly pointed out to use the xrange() iterator not a list creating range().

To choose a sample from a range of integers, use an xrange() object as an >argument. This is especially fast and space efficient for sampling from a large >population: sample(xrange(10000000), 60).

And most certainly you'll have to get the return value of your function somehow stored in a variable. If you do just this

numList = []

Nothing will ever happen to your numList, because your numList only lives within the function scope. You'll have to catch the return value to have it available also in your main-function scope

main()
    numList = []
    numList = getrandomList()
    print(numList)

This should do it.

hallole
  • 179
  • 8
  • Your advice re: xrange doesn't apply to Python 3, where `range` returns a lazily evaluated iterator. (Great advice for Python 2 though) – zehnpaard Mar 02 '15 at 21:55
  • Ou right, it's even specified in the post-title, didn't see that, I am usually using 2.7 therefor I forgot about that, but that's right @zehnpaard – hallole Mar 02 '15 at 21:56