python lottery simulation

Solutions on MaxInterview for python lottery simulation by the best coders in the world

showing results for - "python lottery simulation"
Wilfred
25 Aug 2017
1import random
2
3correct = 0
4total = 0
5run = 1
6
7
8howmany = int(input("How many correct guesses should there be? (0-6) "))
9if howmany < 0:
10    print("That was below 0.")
11    quit()
12elif howmany > 6:
13    print("That was over 6.")
14    quit()
15
16runs = int(input("How many runs should there be? "))
17if runs < 1:
18    print("There has to be at least one run.")
19    quit()
20
21
22while run <= runs:
23    correct = 0
24    howoften = 0
25    while correct != howmany:
26        correct = 0
27        guess = []
28        drawing = []
29
30        while len(guess) < 6:
31            if len(guess) < 6:
32                number = random.randint(1, 50)
33                if not number in guess:
34                    guess.append(number)
35        while len(drawing) < 6:
36            if len(drawing) < 6:
37                number2 = random.randint(1, 50)
38                if not number2 in drawing:
39                    drawing.append(number2)
40
41        x = 0
42        y = 0
43        while y < len(guess):
44            while x < len(guess):
45                if guess[x] == drawing[y]:
46                    correct += 1
47                x += 1
48            y += 1
49            x = 0
50        howoften += 1
51        print("run", (run), "| draw ",
52              howoften, "| correct", correct)
53    total += howoften
54    run += 1
55
56
57average = round((total / runs), 2)
58
59
60print("To get", howmany, "correct guesses", runs, "times, there had to be a total of", total, "tries. That's an average of ", average, "tries per run. (so the chance is 1:" + str(average) + ")")
61input("Hit Enter to exit")