Python practice questions

Practice Questions

Lottery example:

  • Pick(Print) 6 random numbers without the duplication within the range between 1 and 45.

  • python code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import random
lotto_list = []
index = []
lotto = []
for i in range(6):
index.append(i)
lotto_list.append(random.randint(1, 45))
for n, m in zip(index, lotto_list):
lotto.append(m)
cnt = lotto.count(lotto[n])
if cnt >= 2:
lotto.remove(lotto[n])
lotto.append(random.randint(1, 45))
else:
lotto = lotto
lotto

Similar question:

  • Make 5 sets for a lottery.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
total_lotto = []
for i in range(5):
lotto_list = []
index = []
lotto = []
for i in range(6):
index.append(i)
lotto_list.append(random.randint(1, 45))
for n, m in zip(index, lotto_list):
lotto.append(m)
cnt = lotto.count(lotto[n])
if cnt >= 2:
lotto.remove(lotto[n])
lotto.append(random.randint(1, 45))
else:
lotto = lotto

if len(lotto) == 6:
break
total_lotto.append(lotto)
total_lotto