2
\$\begingroup\$

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("Your guesses:")
print(*guessed)
print("\n")
print("You guessed " + str(len(correct)))
\$\endgroup\$
4
\$\begingroup\$
  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)))
    

About lambda see https://docs.python.org/2/reference/expressions.html#lambda

  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.

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

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:
        print('Please enter a number')
        continue
    if gnum > 50:

This allows you to possibly get an input that is not a number, and when it tries to turn the string into an int and it doesn't work, it pulls a ValueError, but you're expecting a ValueError and you instead of crashing the program, you print a thing and skip the rest of that loop so nothing gets added, and it begins again at the question.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.