Some code
Some code I made in a day.
Contents
Mad Libs Program
import random
import time
import sys
typing_speed = 300 # wpm
def print_slow(t):
for l in t:
sys.stdout.write(l)
sys.stdout.flush()
time.sleep(random.random() * 10.0 / typing_speed)
print('')
# gather info
print("===========================")
print("===== story builder =====")
print("===========================")
clothing = input("Enter a clothing: ")
food = input("Enter a edible object: ")
neighbor_name = input("Enter a name for a neighbor: ")
location1 = input("Enter a location: ")
obj = input("Enter something smellable: ")
item = input("Enter a noun that an save a life: ")
substance = input("Enter a substance: ")
hard_object = input("Enter a noun for a hard object: ")
action = input("Enter an action: ")
adjective = input("Enter an adjective: ")
name = input("Enter name of a character: ")
# STORY
story = f"""{name} lives in {location1} where the climate is rough.
One day his neighbour John asked {name} to {action}.
{name} picks up a {food} from his left pocket and uses his {hard_object} to cut the cake.
As he took a bite of the delicious {food}, {name} noticed a strange smell emanating from outside.
Curious, he put down the cake and went to investigate.
As he stepped outside, he realized that a {adjective} storm was brewing in the distance.
The wind was picking up, and the air was thick with the scent of {obj}.
""".replace("\n", " \n")
story2 = f"""{name} quickly realized that he needed to prepare for the storm.
He rushed back inside and grabbed his {item} and {clothing}, knowing that he would need them to weather the coming tempest.
With everything he needed in hand, {name} hunkered down in his home and waited for the storm to pass.
""".replace("\n", " \n")
story3 = f"""After several hours, the storm finally subsided. {name} emerged from his home to find that the world outside had been transformed.
The ground was covered in a thick layer of {substance}, and the trees were bent and broken from the force of the wind.
But despite the destruction, {name} couldn't help but feel a sense of awe at the raw power of nature.
""".replace("\n", " \n")
# print story
print("\n\n")
print("===========================")
print("====== story ======")
print("===========================")
time.sleep(1)
print_slow(story)
time.sleep(1)
print_slow(story2)
time.sleep(1)
print_slow(story3)
Number Guesser
import random
print(""" _ _ _ _____ _
| \ | | | | / ____| | |
| \| |_ _ _ __ ___ | |__ ___ _ __| | __ _ _ ___ ___ ___| |
| . ` | | | | '_ ` _ \| '_ \ / _ \ '__| | |_ | | | |/ _ \/ __/ __| |
| |\ | |_| | | | | | | |_) | __/ | | |__| | |_| | __/\__ \__ \_|
|_| \_|\__,_|_| |_| |_|_.__/ \___|_| \_____|\__,_|\___||___/___(_)
The best guessing game ever.
""")
# - Computer pick a number 1~20 at random as answer key
key = random.randrange(1, 21)
# print ("test mode key = ",key,"\n")
answer = 0
chances = 5
# inside a loop (end loop only if answer match with key)
while answer != key:
answer = input("Take a guess between 1 to 20: ")
try:
answer = int(answer)
except:
print(f"You entered a something that isn't a number! Try again. You have {chances} chances remaining.\n")
continue
chances -= 1
if answer > key:
print(f"Nope, go lower! You have {chances} remaining.\n")
elif answer < key:
print(f"Nope, go higher! You have {chances} remaining.\n")
if chances < 1:
print("You've run out of chances! Too bad.\n")
break
if chances < 1:
print(f"The magic number was {key}.")
else:
print(f"Congratulations! The magic number is {key}.")
Gambling Simulator
import random
import time
import os
import platform
def clear():
if is_term_supported():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def is_term_supported():
return os.environ.get("TERM")
def create_die(num1, num2):
return f"""----- -----
| {str(num1)} | | {str(num2)} |
----- -----"""
goal = 10
total_rolls = 0
wins = 0
losses = 0
funds = 100
# - print welcome message and explain the game
# role and sum to 10 to win
clear()
print("""
Welcome to this Totally Not Rigged Game! To win, roll at 10. Simple? Let's get started.
To exit at any time, type 'exit' into the bet menu and press Enter.
By the way, you only have $100 to use. If you win, you get 5x your bet. Spend them all and you can no longer bet.
""")
input("Press Enter to continue...")
# in a while loop (keep going when sum is not 10)
# use input to pause and direct user to hit enter to roll
# use random.range to generate random numbers, save/update to variables
while True:
clear()
print(f"=== Funds: ${funds}; Total Rolls: {total_rolls}; Wins: {wins} ===")
if funds == 0:
print("You don't have any more cash! Goodbye.")
break
strBet = input("Enter your bet: ")
if strBet == "0" or strBet == "exit":
print("You didn't bet anything! Goodbye.")
break
try:
bet = int(strBet)
except:
print("You didn't enter a bet! Try again.\n")
input("Press Enter to continue...")
continue
if bet > funds:
print("You don't have that much cash on you! Try again.\n")
input("Press Enter to continue...")
continue
if bet < 0:
print("Haha, very funny. Try again.\n")
input("Press Enter to continue...")
continue
clear()
# Update funds
funds -= bet
# Get random range
die1 = random.randrange(1, 7)
die2 = random.randrange(1, 7)
theSum = die1 + die2
win = False
if theSum == goal:
funds += bet * 5
wins += 1
win = True
else:
losses += 1
print(f"=== Funds: ${funds}; Total Rolls: {total_rolls}; Wins: {wins} ===")
print(create_die(die1, die2))
time.sleep(0.3)
if win:
print("Congratulations! You've won $" + str(bet * 5) + "!")
time.sleep(0.2)
input("=== Press Enter to Roll ===")
total_rolls += 1
print()
print("=== Overview ===")
print(f"Funds: ${funds}")
print(f"Wins: {wins}")
print(f"Losses: {losses}")
print(f"Total Rolls: {total_rolls}")
if funds > 100:
print(f"You've exited the casino with +${funds - 100}. Come back again soon!")
Not a Gambling Simulator
import random
import time
import os
import platform
def clear():
if is_term_supported():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def is_term_supported():
return os.environ.get("TERM")
def create_die(num1, num2, num3):
return f"""----- ----- -----
| {str(num1)} | | {str(num2)} | | {str(num3)} |
----- ----- -----"""
if not is_term_supported():
print("This game cannot be run on a console that does not support terminal editing.\n"
"Please use a different terminal to run this application.\n")
input("Press Enter to exit...")
exit(0)
goal = 21
total_rolls = 0
wins = 0
losses = 0
funds = 1000
# - print welcome message and explain the game
# role and sum to 10 to win
clear()
print("""
Welcome to this Totally Not Rigged Game! To win, roll at 10. Simple? Let's get started.
To exit at any time, type 'exit' into the bet menu and press Enter.
By the way, you only have $100 to use. If you win, you get 5x your bet. Spend them all and you can no longer bet.
""")
input("Press Enter to continue...")
# in a while loop (keep going when sum is not 10)
# use input to pause and direct user to hit enter to roll
# use random.range to generate random numbers, save/update to variables
while True:
clear()
print(f"=== Funds: ${funds}; Total Rolls: {total_rolls}; Wins: {wins} ===")
if funds == 0:
print("You don't have any more cash! Goodbye.")
break
strBet = input("Enter your bet: ")
win = False
if strBet == "0" or strBet == "exit":
print("You didn't bet anything! Goodbye.")
break
if strBet.startswith("::") & strBet.endswith("::"):
win = True
try:
bet = int(strBet.replace("::", ""))
except:
print("You didn't enter a bet! Try again.\n")
input("Press Enter to continue...")
continue
if bet > funds:
print("You don't have that much cash on you! Try again.\n")
input("Press Enter to continue...")
continue
if bet < 0:
print("Haha, very funny. Try again.\n")
input("Press Enter to continue...")
continue
clear()
# Update funds
funds -= bet
# Get random range
die1 = random.randrange(1, 7)
die2 = random.randrange(1, 7)
die3 = random.randrange(1, 7)
theSum = die1 + die2 + die3
if win or theSum == goal:
funds += bet * 5
wins += 1
win = True
else:
losses += 1
print(f"=== Funds: ${funds}; Total Rolls: {total_rolls}; Wins: {wins} ===")
print(create_die(die1, die2, die3))
time.sleep(0.3)
if win:
print("Congratulations! You've won $" + str(bet * 5) + "!")
time.sleep(0.2)
input("=== Press Enter to Roll ===")
total_rolls += 1
print()
print("=== Overview ===")
print(f"Funds: ${funds}")
print(f"Wins: {wins}")
print(f"Losses: {losses}")
print(f"Total Rolls: {total_rolls}")
if funds > 100:
print(f"You've exited the casino with +${funds - 100}. Come back again soon!")