# 1-50 lottery game

I am a beginner Python "developer" and I was messing with Python while I was offline on vacation, so I made this lottery game. Please leave your opinions on what I should do to improve it.

import random
guessed=[]
correct=[]
combo=[]
numtimes=6
loop = 0
#generates 7 random numbers and adds to list
while loop < 7:
nmbr = random.randint(1,50)
loop += 1
combo.append(nmbr)
combo.sort()
print("Choose seven numbers:")
#number picking system
while len(guessed) < 7:
gnum = int(input())
if gnum > 50:
print("The number cant be more than 50!" + "\nEnter another one:")
continue
elif gnum in guessed:
print("You already guessed that! Try again!")
continue
#keeps track of how much nums are inputted
else:
guessed.append(gnum)
print("You need " + str(numtimes) + " more!")
numtimes -= 1
print("Numbers drawn:")
print(*combo)
#checks for correct guesses and adds them to a correct list
for num in combo:
if num in guessed:
if num in correct:
continue
else:
correct.append(num)
print(*guessed)
print("\n")
print("You guessed " + str(len(correct)))


1. All your code to create 7 random numbers, i.e.:

loop = 0
#generates 7 random numbers and adds to list
while loop < 7:
nmbr = random.randint(1,50)
loop += 1
combo.append(nmbr)
combo.sort()


you can rewrite as:

#generates 7 random numbers and adds to list
combo = sorted(map(lambda x: random.randint(1,50), range(0,7)))


1. Calculate count or correct values:

#checks for correct guesses and adds them to a correct list
for num in combo:
if num in guessed:
if num in correct:
continue
else:
correct.append(num)


Rewrite as:

correct = list(set([val for val in guessed if val in combo]))


In this code you receive all values (val) from combo, if value (val) exist in guessed. And from all this values you create list (correct). list and set were used to create list with uniq values.

2. All constants (7, 50, etc) replace to variable.

• Oh crap, thanks for pointing that out. I was unaware of the use of "lambda" function, so I didn't think of implementing it. – exali Nov 12 '17 at 22:19
• can you explain the second piece of code? – exali Nov 12 '17 at 22:29
• @exali he is just taking the inputs that are in the extracted random numbers and removing duplicates with set – Caridorc Nov 19 '17 at 19:49

If you are trying to make this open to human error I have one suggestion

while len(guessed) < 7:
gnum = int(input())
if gnum > 50:


I might rewrite that bit of code as

while len(guessed) < 7:
try:
gnum = int(input('What\'s your lucky guess??\n-->'))
except ValueError: