0
 import random def randomNum(): for iteration, num in enumerate(range(4)): first = random.randint(1,10) second = random.randint(1,10) third = random.randint(1,10) fourth = random.randint(1,10) print('Lotto number 1', first) print('Lotto number 2', second) print('Lotto number 3', third) print('Lotto number 4', fourth) print('the number of tries', iteration) print(randomNum()) 

I'm new to python and I was given some instructions to create a lottery program but I don't even know where to start with this. the instructions are to 1-create a program that picks for random numbers. 2-Make it so the program does not pick the same number twice(the one that confused me). 3- count the number of retries to get four different numbers. 4- make it so you can keep playing it. Included a picture of the results

enter image description here

3
  • I've been working on this for a while now and I can't seem to get anywhere with it
    – Mozarts
    CommentedNov 9, 2020 at 19:06
  • Recommendation: Kick it old school and grab a pencil and paper. Draw out the logic step by step (I.e: a flow chart). Once happy with the logic flow, implement (write a little bit of code to fulfill that step) one step at a time. (I use this technique on a daily basic, works for beginners and experienced alike). All the best!
    – s3dev
    CommentedNov 9, 2020 at 19:10
  • Agreed that you should try this on paper before even trying to code it. You need to think about all logical paths your program could take. Maybe just think about what output you want given every possible sequence of user input. As-is, your problem statement isn't clear enough for us even if we wanted to code up the entire thing for you, since you didn't specify how the guessing works - is the user supposed to guess 1 of the 4 numbers, or all 4 at once? Are they supposed to keep guessing 1 at a time? Are they actually supposed to be guessing just one 4-digit number?CommentedNov 9, 2020 at 19:15

2 Answers 2

2

This can help:

import random retries = 0 def randomNum(): my_nums = [] for i in range(4): my_nums.append(random.randint(1,10)) # check duplicates here. if len(set(my_nums)) < len(my_nums): global retries retries+=1 randomNum() else: lotto_number = 1 for items in my_nums: print("Lotto number {}".format(lotto_number), items) lotto_number+=1 print("The number of tries", retries) 
  • The time complexity becomes O(n^2) here but considering the small amount of data, this should be good enough. I would get the second for loop outside the function if data is projected to be massive. Space Complexity is O(n) which is acceptable.
  • To change the number of lottos you can change integer in the range().
  • 'Global' is not advisable to be used whenever possible and there are different solutions to that too but for a quick solution, this should be fine.
  • I'm checking the duplicates by checking the length of array vs the length of set of that array. After creating set of the array, if it is smaller than the array, that basically signifies that we had duplicate values in the array.

Sample output

    1

    1) You've done the random number generation

    2) Distinct Random Numbers To get distinct numbers you will have to loop over the list every time you generate a number and check if the number already exists.

    def checkDistinct(random, nums): for i in range(0, len(nums)): if random == nums[i]: return False return True def getLottoNums(): nums = [randint(1, 10)] # No need to check if distinct for i in range(1, 4): # 1, 2, 3 r = randint(1, 10) while not checkDistinct(r, nums): r = randint(1, 10) # reroll if not distinct nums.append(r) return nums 

    3) Count number of retries You can simple add a global retries variable. declare it in the getLottoNums() function using the global keyword like global retries. Then you can increment it each time checkDistinct returns false.

    retries = 0 # You can just declare this once in the while loop in part 4 def getLottoNums(): global retries nums = [randint(1, 10)] .... while not checkDistinct(r, nums): retries += 1 .... 

    4) Make it so you can keep playing it. Simply wrap it inside a while loop which keeps running until input is something other than y. This is the main code outside of all functions:

    inp = 'y' while(inp == 'y'): retries = 0 nums = getLottoNums() for i in range(0, 4): print(f"Lotto number {i+1} {nums[i]}") print(f"It took {retries} retries to get the four different numbers") inp = input("Do you want to play again? (y)") 
    1
    • Downvoted for using global variables, and for ‘handing out’ the answer rather than encouraging self learning and the value of research.
      – s3dev
      CommentedNov 9, 2020 at 19:47

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.