List with Random Numbers - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: List with Random Numbers (/thread-5632.html) |
List with Random Numbers - AnjyilLee - Oct-14-2017 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_listbut I keep getting this error: Why does it think I haven't defined it? And how can I improve my program?
RE: List with Random Numbers - metulburr - Oct-14-2017 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) RE: List with Random Numbers - AnjyilLee - Oct-14-2017 (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. Thank you very much! I feel like I'm always overlooking the most simplest things. RE: List with Random Numbers - buran - Oct-14-2017 Did you cover list comprehensions in class? RE: List with Random Numbers - AnjyilLee - Oct-14-2017 (Oct-14-2017, 07:03 AM)buran Wrote: Did you cover list comprehensions in class? List comprehensions? Well, we've talked about lists, yes. RE: List with Random Numbers - buran - Oct-14-2017 (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... |