Python Forum
List with Random Numbers
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List with Random Numbers
#1
For my homework, I need to make a lottery number generator, a program that generates a 7 digit lottery number, each digit generated on their own and between 0-9, then puts them in a list. By looking through my textbook and assignments done in class, I was able to piece this together:
def main():
    lottery_list = []
    import random
    random.seed(10)
    random1 = random.randint(0, 9)
    lottery_list.append(random1)
    random2 = random.randint(0, 9)
    lottery_list.append(random2)
    random3 = random.randint(0, 9)
    lottery_list.append(random3)
    random4 = random.randint(0, 9)
    lottery_list.append(random4)
    random5 = random.randint(0, 9)
    lottery_list.append(random5)
    random6 = random.randint(0, 9)
    lottery_list.append(random6)
    random7 = random.randint(0, 9)
    lottery_list.append(random7)

main()


print lottery_list
but I keep getting this error:
Error:
Traceback (most recent call last): File "C:/Users/Sami3_000/Documents/School/Intro to Program Logic and Design/Ch7Ex2.py", line 28, in <module> print lottery_list NameError: name 'lottery_list' is not defined
Why does it think I haven't defined it? And how can I improve my program?
Reply
#2
lottery_list is created in the function and only known in the function. You want to return that.

Move the import out of the function.

Loop the randoms numbers to remove redundancy

You already did the work, just forgot to return lottery_list, so here is your code shortened as an example.
import random

def main():
    lottery_list = []
    for i in range(7):
        lottery_list.append(random.randint(0,9))
    return lottery_list
    
lst = main()
print(lst)
Recommended Tutorials:
Reply
#3
(Oct-14-2017, 02:52 AM)metulburr Wrote: lottery_list is created in the function and only known in the function. You want to return that.

Move the import out of the function.

Loop the randoms numbers to remove redundancy

You already did the work, just forgot to return lottery_list, so here is your code shortened as an example.
import random

def main():
    lottery_list = []
    for i in range(7):
        lottery_list.append(random.randint(0,9))
    return lottery_list
    
lst = main()
print(lst)

Thank you very much! I feel like I'm always overlooking the most simplest things.
Reply
#4
Did you cover list comprehensions  in class?
Reply
#5
(Oct-14-2017, 07:03 AM)buran Wrote: Did you cover list comprehensions  in class?

List comprehensions? Well, we've talked about lists, yes.
Reply
#6
(Oct-14-2017, 09:17 PM)AnjyilLee Wrote: List comprehensions? Well, we've talked about lists, yes.
OK, can you try and make the same code as list comprehension? as an exercise...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,419 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,004 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Return the sum of the first n numbers in the list. pav1983 3 4,092 Jun-24-2020, 03:37 AM
Last Post: deanhystad
  numbers in list - max value and average stewie 2 2,827 Apr-01-2020, 06:25 PM
Last Post: buran
  Random selection from list Mohsky 1 10,175 Mar-31-2020, 10:27 PM
Last Post: deanhystad
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,719 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  Scaling random numbers within specific range schniefen 4 3,192 Oct-28-2019, 11:22 AM
Last Post: jefsummers
  How to Generate and Print An Array with Random Numbers in Python in the johnnynitro99293 14 9,629 Sep-20-2019, 11:56 AM
Last Post: ichabod801
  list of odd numbers cffiver2 2 5,193 Jul-12-2019, 09:46 AM
Last Post: perfringo
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,293 Dec-19-2018, 06:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020