## Web/Tech

##### 02/18/2022

(So, I'm writing this as Storm Eunice rages outside, and being constantly frustrated by the whistling its making, so that's why myself will be very angry through the duration of this post. Also, this is adapted from CGP Greys video This video will make you angry.)

## How To Make A Slot Machine Game In Python

##### 03/02/2021

So, I am currently working on a project called Henry's Palace (after Caesars Palace in Las Vegas) on Python. What was a simple dice game to teach m how to code probability has ballooned into 800 lines of code. recently, I added a huge extension, the slot machine. As I had trouble doing this I will show you my personal code for copy and pasting. You'll have to add spaces yourself.

sym1 = random.choice(symbols)
sym2 = random.choice(symbols)
sym3 = random.choice(symbols)
print("---Symbols---")
print(sym1, sym2, sym3, "\n")
if (sym1 == sym2 or sym1 == sym3 or sym2 == sym3) and not(sym1 == sym2 == sym3):
print("Winner! +5p")
credit += 45
return credit

elif (sym1 == sym2 == sym3) and sym1 != "Bell":
print("Winner! +£50")
credit = credit + 50
return credit

elif (sym1 == sym2 == sym3) and (sym1 == "Bell"):
print("Jackpot! +£15")
credit = credit + 50
return credit

elif (sym1 == sym2 == "Skull" or sym1 == sym3 == "Skull" or sym2 == sym3 == "Skull") and not(sym1 == sym2 == sym3):
print("Two Skulls! -£75")
credit = credit - 100
return credit

elif (sym1 == sym2 == sym3) and sym1 == "Skull":
print("Three Skulls! Lose all credit")
credit = 0
return credit

else:
print("Loser! -£20")
credit = - 20
return credit

symbols = ["Cherry", "Bell", "Lemon", "Orange", "Star", "Skull"]
print("You have", credit, "p", "\n")

#moneydrinker function
def moneydrinker(credit):
while True:
print("")
play = input("Roll costs 10p. Would you like to roll? yes/no: ")
print("")
if play == "yes" or "y" or "Yes" or "Y":
if credit >= 20:
credit=roll(credit)
credit -= 10
print("Credit is", credit, "p")
if credit < 20:
print ("You need more points! Time to go home,right?")
else:
print("You do not enough money to roll!")
if intro==1:

## Why is this code faster with Python than with C++?

##### 03/01/2021

I wrote a simple code to compare the speed of Python vs. C++. Here is the Python code:

``````def count_permutations(N:int)->int:
count=0
for p in itertools.permutations(range(1,N+1)):
count += 1
return count
``````

And here is the C++ code:

``````int count_permutations(int N) {
vector<int> v(N);
iota(v.begin(), v.end(), 1); // fill the vector with 1,...,N
int count=0;
do {
++count;
} while ( next_permutation(v.begin(),v.end()) );
return count;
}
``````

When I run with Python 3.8.5 on Ubuntu, I get:

``````Permutations of 1..11:
39916800 permutations calculated in 3.31172251701355 seconds
Permutations of 1..12:
479001600 permutations calculated in 40.63520336151123 seconds
``````

When I run with C++ (clang++-9) on the same Ubuntu, I get:

``````Permutations of 1..11:
39916800 permutations calculated in 5 seconds
Permutations of 1..12:
479001600 permutations calculated in 57 seconds
``````

I find this very strange, since in the benchmarks website, C++ is always faster than Python.

Is it possible that recent versions of Python have become so fast that they are now faster than their C++ parallel?