1
\$\begingroup\$

I have written a program in C that tests how many attempts it would need to win a lottery. Now I wanted to try out Python and I have written a little game based on the 6 out of 49 lottery game. I would be happy with advice and bug reports.

import random
from pathlib import Path

print("This program simulates a Lottery\n")
print("Type \"help\" for information")

# initialzise stats
if(Path("./save.txt").is_file()):
    save_file = open("./save.txt", "r")
    money = int(save_file.readline())
    money_won = int(save_file.readline())
    money_lost = int(save_file.readline())
    won_2 = int(save_file.readline())
    won_3 = int(save_file.readline())
    won_4 = int(save_file.readline())
    won_5 = int(save_file.readline())
    won_6 = int(save_file.readline())
    lost = int(save_file.readline())
    paid = int(save_file.readline())


else:    
    money = 1000
    money_won = 0
    money_lost = 0
    won_2 = 0
    won_3 = 0
    won_4 = 0
    won_5 = 0
    won_6 = 0
    lost = 0
    paid = 0

def won_total():
    return won_2 + won_3 + won_4 + won_5 + won_6

# save stats, called when program gets quitted correctly and when money is not enough
# to play again
def save():
    file = open("./save.txt", "w")
    file.write(str(money) + "\n")
    file.write(str(money_won) + "\n")
    file.write(str(money_lost) + "\n")
    file.write(str(won_2) + "\n")
    file.write(str(won_3) + "\n")
    file.write(str(won_4) + "\n")
    file.write(str(won_5) + "\n")
    file.write(str(won_6) + "\n")
    file.write(str(lost) + "\n")
    file.write(str(paid) + "\n")
    file.close()

# start of game
while True:
    print()
    command = input("> ")
    print()

    if command == "help":
        print("help - display useful information")
        print("stat - show statistics")
        print("play - try your luck")
        print("quit - quit program (saves statistics)\n")
        print("if you want to play a new game delete the save file")

    elif command == "stats":
        print("Money : " +str(money) + "\n")
        print("Money won  : " + str(money_won))
        print("Money lost : " + str(money_lost) + "\n")
        print("Games won  : " + str(won_total()))
        print("Games lost : " + str(lost) + "\n")
        print("2 right: " + str(won_2))
        print("3 right: " + str(won_3))
        print("4 right: " + str(won_4))
        print("5 right: " + str(won_5))
        print("6 right: " + str(won_6))

    elif command == "play":
        if money >= 2:
            money -= 2
        else:
            print("You dont have enough money!")
            save()
            break

        draw = sorted(random.sample(range(1,50), 6))
        tries = []
        for i in range(6):
            tries.append(int(input("Number " + str(i + 1) + ": ")))

        right_numbers = 0
        if tries[0] in draw: right_numbers += 1 
        if tries[1] in draw: right_numbers += 1
        if tries[2] in draw: right_numbers += 1
        if tries[3] in draw: right_numbers += 1
        if tries[4] in draw: right_numbers += 1
        if tries[5] in draw: right_numbers += 1

        print("The draw was: " + str(draw))
        print("Right numbers: " + str(right_numbers))

        money_won_currently = 0
        if right_numbers <= 1:
            print("You won nothing.")
            lost += 1
            money_lost += 2

        elif right_numbers == 2:
            money_won_currently = 5
            print("You won 5 dollars!")
            won_2 += 1

        elif right_numbers == 3:
            money_won_currently = 220
            print("You won 220 dollars!")
            won_3 += 1

        elif right_numbers == 4:
            money_won_currently = 5000
            print("You won 5.000 dollars!")
            won_4 += 1

        elif right_numbers == 5:
            money_won_currently = 190000
            print("You won 190.000 dollars!")
            won_5 += 1

        elif right_numbers == 6:
            money_won_currently = 20000000
            print("You won 20.000.000 dollars!")
            won_6 += 1

        money_won += money_won_currently
        money += money_won_currently

    elif command == "quit":
        save()
        break

    else:
        print("unknown command")
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I suppose you want to review your code...

So, first:

In Python you may use context manager to work with files - it's a more straightforward and a safer approach, too. So instead of

save_file = open("./save.txt", "r")
money = int(save_file.readline())
money_won = int(save_file.readline())
money_lost = int(save_file.readline())
won_2 = int(save_file.readline())
won_3 = int(save_file.readline())
won_4 = int(save_file.readline())
won_5 = int(save_file.readline())
won_6 = int(save_file.readline())
lost = int(save_file.readline())
paid = int(save_file.readline())

you may write

with open("./save.txt", "r") as save_file:
    money = int(save_file.readline())
    money_won = int(save_file.readline())
    money_lost = int(save_file.readline())
    won_2 = int(save_file.readline())
    won_3 = int(save_file.readline())
    won_4 = int(save_file.readline())
    won_5 = int(save_file.readline())

and your file will be closed automatically for you, even in the case of throwing an exception.


Second - why don't use a list instead of 5 similar variables won_2, won_3, won_4, won_5, won_6? Then you may utilize the advantage of for loops instead of work individually with every one of them, something as in the following part of my answer:


Third, instead of

    if tries[0] in draw: right_numbers += 1 
    if tries[1] in draw: right_numbers += 1
    if tries[2] in draw: right_numbers += 1
    if tries[3] in draw: right_numbers += 1
    if tries[4] in draw: right_numbers += 1
    if tries[5] in draw: right_numbers += 1

you may simply write

for i in range(6):
    if tries[i] in draw:
        right_numbers += 1 

Note the splitting your one-line if statement into 2 lines, in concordance with PEP 8 - Style Guide for Python Code.


Fourth, why to reinvent the wheel? Use some of ready for use command-line parsing solutions, e. g. the argparse module in the Python standard library.

\$\endgroup\$
1
\$\begingroup\$

First, in your initialize stats section, I would replace your if-then-else code with try-except: try to read a file, and if it fails, use the default values. I would also print out whether you're using the default values or not, so if there's a problem with the save file, it won't fail silently. As MarianD says, your won variables should be in a list. Then I would get rid of won_total(); I'm generally not a fan of one-line functions. You can replace it with sum(won_list). You can then do

for won_number in range(2,7): 
  print("{} right: {}".format(won_number,won_list[won_number]))

You can also make your program more general and avoid "magic numbers" by replacing range(2,7) with range(2,draw_size+1). After if right_numbers <= 1: you have a bunch of elif. You can replace them all by creating a dictionary prize_dict = {2:5,3:220,4:2000,5:190000,6:20000000} and then doing

money_won_currently = prize_dict[right_numbers]
print("You won {} dollars!".format(money_won_currently)
won_list[right_numbers] += 1

You can also calculate the number of right numbers with sum([right_number for right_number in tries if right_number in draw]. Alternatively, you don't really need to store your tries to begin with; just put

if int(input("Number " + str(i + 1) + ": ")) in draw: 
  right_numbers += 1

in the for-loop that has the user enter the numbers. The only reason to store the numbers would be to check whether the user is entering unique numbers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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