Daily / How To? · October 4, 2022 0

‘Python Lottery’ Explanation

Key: italic is for python functions and bold is for variables.

# deciding if the lottery is a benefit

import random

MyChoice=[12, 33, 4, 6, 46, 29, 50]

balance = 1000 # I will spend this much money for the lottery
moneyIWon = 0
timesPlayed = 0
while balance >= 5:
    timesPlayed += 1
    balance -= 5
    result = []
    while len(result) < 7:
        thisResult = random.randint(1, 50)
        if thisResult not in result:
            result.append(thisResult)

    myGuess = 0
    for i in MyChoice:
        if i in result:
            myGuess += 1

    if myGuess == 3:
        balance += 5
        moneyIWon += 5
        print("I won 5 dollars!")
    elif myGuess == 4:
        balance += 20
        moneyIWon += 20
        print("I won 20 dollars!")

    elif myGuess == 5:
        balance += 106
        moneyIWon += 106
        print("I won 106 dollars!")

    elif myGuess == 6:
        balance += 5351
        moneyIWon += 5351
        print("I won 5351 dollars!")

    elif myGuess == 7:
        balance += 70000000
        moneyIWon += 70000000
        print("I won 70000000 dollars! Oh, my!")
        break


# conclusion

print('I tried',timesPlayed,'times, spent',timesPlayed*5,'dollars and the money I have left is', balance, 'dollars.')
print('I got', moneyIWon, 'dollars by buying lottery tickets, but my profit was spent.')

# end of code

First: The # signs represent a comment. Comments can help someone understand the code. It’s like a silvery ghost. import random indicates that random will be used in this code to help choose random numbers for the lottery. When you don’t use this, import random is gray. Alright, MyChoice is a set of numbers I randomly picked. It doesn’t have to be exactly that set of numbers, but I chose that set. You’ll see why soon…

Balance tells the money I have. Right now I pretend I have $1000. You may change the value into whatever you wish-68, 53302, 1, 20.43, 3.14159265358979323, 100000009… MoneyIWon means, well, money you won. TimesPlayed is of course the times I played the lottery. And just about 5 coins will buy you a ticket. So, while you can afford a ticket (while balance >= 5:), you play 1 more time, and you add one to your timesPlayed bag. And you lose your precious 5 coins. (Perhaps cash!) So the balance bag is subtracted by 5.

Result, that’s the true lottery numbers. Like, if your guess is 34, 45, 1, 39, 12, 21, and 7, but the answer is 43, 50, 3, 9, 35, 31, and 49, then that is result. Right now, this list is plain. You pick 7 numbers. When you have picked these 7, this code stops. Now, the random.randint(1, 50) shows the random way- it picks a number from 1 to 50 for result.

MyGuess says how many numbers I guessed correct. Reading the code, if I get 3 correct I get 5 dollars, which can help me get a large box of glazed treats. If I get 4 correct, I get 20 dollars, which can buy me a warm winter coat. And $106 can buy me a really nice gift for someone. $5351, well, that can buy a simple phone, right? But then, after that, I can buy 10 boxes of treats, 10 winter coats, 10 really nice gifts, 10 simple phones, and still have money for a new mansion and furniture! Okay. Back from the break.

For the conclusion.

print('I tried',timesPlayed,'times, spent',timesPlayed*5,'dollars and the money I have left is', balance, 'dollars.')
print('I got', moneyIWon, 'dollars by buying lottery tickets, but my profit was spent.')

We draw the conclusion. If you run this code, your conclusion will be something like this.

I tried 215 times, spent 1075 dollars and the money I have left is 0 dollars.
I got 75 dollars by buying lottery tickets, but my profit was spent.

We tell Python to print out how many times we tried to win money from the lottery. Next, we calculate how much money we spent. And we are informed how much money you got and the very final result. But why timesPlayed X 5? Well, try to remove the X 5.