Day 3 assignment – Apostrophe’s

Today’s exercise is a simple text based adventure. Dr Angela Yu of the Udemy Course 100 Days of Code: The Complete Python Pro Bootcamp for 2022 provided the basic framework. I just need to copy the text, embellish it a bit, and add in the flow control to navigate through the game, resulting in a dead player, or escape the labyrinth. There are no hints on how to navigate the game to win, you need to read the code, or look at the example below.

What is the difference between ‘ and ’ one is an ascii single quote that is used by code to indicate encapsulated text is a string, and the second one is punctuation. It is common to use the single quote. The evil Dr Yu provide the ascii single quote throughout the examples provided. I fortunately have played this game of mismatched single quotes before and converted them first thing.

At this rate I might get through 100 days of code in a couple years, but I’m not in a hurry, and I like the slower pace. I’m also working on blogging. Having something to write everyday is a nice way to practice blog writing.

print("Welcome to Treasure Island.")
print("Your mission is to find the treasure.") 

#https://www.draw.io/?lightbox=1&highlight=0000ff&edit=_blank&layers=1&nav=1&title=Treasure%20Island%20Conditional.drawio#Uhttps%3A%2F%2Fdrive.google.com%2Fuc%3Fid%3D1oDe4ehjWZipYRsVfeAx2HyB7LCQ8_Fvi%26export%3Ddownload

#Write your code below this line 👇
user_input = ""
print('You’re at a crossroad. Where do you want to go?' )
while user_input.lower() != "left":
  user_input = input('Type "left" or "right"')
  if (user_input.lower() == "right"):
    print("You hear a painful cry like a little fluffy cute animal calling out for help, you jump into actual and run to the right, but you should look where you are going because you fell into a deep hole and died dead.")

print('You’re come to a lake. There is an island in the middle of the lake.')
while user_input.lower() != "wait":
  user_input = input('Type "wait" to wait for a boat. Type "swim" to swim across.')
  if (user_input.lower() == 'swim'):
    print("You drowned, and now you're dead")
  

print("You arrive at the island unharmed. There is a house with 3 doors. One red, one yellow and one blue.")
while user_input.lower() != 'yellow': 
  user_input = input('Which colour do you choose?')
  if user_input.lower() == 'red':
    print("It’s a room full of fire. Game Over.")
  elif user_input.lower() == "yellow":    
    print("You found the treasure! You Win!")
  elif user_input.lower() == "blue":
    print("You get attacked by an angry trout. Game Over.")
  else:
    print("You chose a door that doesn’t exist. Game Over.")

This is a successful run through looks like:

Welcome to Treasure Island.
Your mission is to find the treasure.
You’re at a crossroad. Where do you want to go?
Type "left" or "right"left
You’re come to a lake. There is an island in the middle of the lake.
Type "wait" to wait for a boat. Type "swim" to swim across.wait
You arrive at the island unharmed. There is a house with 3 doors. One red, one yellow and one blue.
Which colour do you choose?yellow
You found the treasure! You Win!

This is what happens when you enter incorrect directions:

Welcome to Treasure Island.
Your mission is to find the treasure.
You’re at a crossroad. Where do you want to go?
Type "left" or "right" tom
Type "left" or "right" ;et
Type "left" or "right" lft
Type "left" or "right" left
You’re come to a lake. There is an island in the middle of the lake.
Type "wait" to wait for a boat. Type "swim" to swim across. swm
Type "wait" to wait for a boat. Type "swim" to swim across. swam
Type "wait" to wait for a boat. Type "swim" to swim across. sum
Type "wait" to wait for a boat. Type "swim" to swim across. swim
You drowned, and now you're dead
>

When life gets fast… slow it down

Yeah, I’m dragging my butt through the #100daysofcode program. A warning signal is going off in my head, this is not how you create a new habit. But also, you should not kill yourself to force a new habit. You need it to be easy, otherwise if it becomes hard then you hit resistance, and resistance is a brutal killer of new habits. So if you need to take an extra day to continue on your habit that is okay, but be sure to do the minimum you can everyday. Lets look at what I’ve been working on over the last weekend.

Day 3 – Leap Year

Write a program that works out whether if a given year is a leap year. A normal year has 365 days, leap years have 366, with an extra day in February.

Dr Angela Yu. 100 Days of Code: The Complete Python Pro Bootcamp for 2022

I spent extra time to document the process of figuring this out. When the code gets topsy turvy like this I like to write it out in english, it helps tease out the flaws in my logic. I made Several bugs writing this, like not setting the flag false if it is not divisible by 4. I removed an extra if statement and moved the assumption statement to be prominent at the top.

The last line 25 is a bit of trick, rather than use if then else to print the statement I used the position in the array to print out the right statement. There are two results. False = 0 and True = 1. So I used the positions 0 and 1 in the Array to store the values. When I print the result I use the True/False variable to select which statement to use.

Day 3 – Pizza Shop

Another if else test. I didn’t use elif. I could have used elif for the pizza size, but didn’t feel it needed it to make it better.
What I like about code like this is I can simulate the process. And holy shit this is an epiphany! I should use this to document my processes at work! Let’s stay on task though, I’ll write about that next. Back to why I like this, the person who is taking an order needs to understand that additional pepperoni is a different charge for the size of pizza. So that logic needs to be trained to the new employee. But something like this is never mentioned during training. It’s always added when someone realizes the total is not right, then they go back and tell the new employee about the different rates of pepperoni charges.

I feel like coding is a way to document this better than with a flow chart. I find flow charts are cumbersome to illustrate.

# 🚨 Don't change the code below 👇
print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M, or L ")
add_pepperoni = input("Do you want pepperoni? Y or N ")
extra_cheese = input("Do you want extra cheese? Y or N ")
# 🚨 Don't change the code above 👆

#Write your code below this line 👇
price_small_pizza    = 15
price_medium_pizza   = 20
price_large_pizza    = 25
ad_pepperoni_small   = 2
ad_pepperoni_medplus = 3
ad_ex_cheese         = 1

total_price = 0
if size == 'S' or size == 's' :
    total_price += price_small_pizza
    if add_pepperoni == 'Y' :
        total_price += ad_pepperoni_small
if size == 'M' :
    total_price += price_medium_pizza
    if add_pepperoni == 'Y' :
        total_price += ad_pepperoni_medplus
if size == 'L' :
    total_price += price_large_pizza
    if add_pepperoni == 'Y' :
        total_price += ad_pepperoni_medplus
if extra_cheese == 'Y' :
    total_price += ad_ex_cheese

print(f'Your final bill is: ${total_price}.')

Code as documentation brain spill

I don’t like documentation to explain processes. The usual process is that I need to write documentation, then I share it around. People usually look it over but don’t take time to read it and process it. I end up having to verbally explain nuances in a meeting and people look at me weird, then agree it’s fine, and the documentation is never brought up again, until someone needs it. Then we may update it. The greater problem with this is it is not a suitable documentation method. If I wrote a simple javascript program in an HTML page then people could simulate the process.
I’m thinking the ticketing process for new laptops. I could create a text based adventure game for creating a new laptop request, and simulate it through to the end. I could write a simulator for the whole process. Cool!

So I tried to write a simple demo, but there is overhead of interacting with the end user. So I found a terminal emulator library here:
https://terminal.jcubic.pl/#demo
I’ll take a look at this to see if I can use it…

Day 3 Love Calculator

Another day IRL has passed. Meanwhile I’m still on day three of the python programming course. Aside from not feeling well, I enjoy taking the time to play with the code somewhat. For this lab I mixed up what I was supposed to be tallying. So I needed to add in some debug messages.

print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")

lower_names = [name1.lower(), name2.lower()]
test_words = ["true","love"]
match_res = []
total_score = 0
##### Test how many times each letter in the test word matches in a name
for aword in test_words:                           # Cycle through the test words
    letter_hits = 0                                # Reset the word hit counter
    for aname in lower_names:                      # Cycle through the names
        for wletter in aword:                      # Cycle through the letters of word
            countres = aname.count(wletter)        # Count instances of letter
            letter_hits += countres                # Add it to tally
            #print("{} finds {} of {}".format(aname,countres,wletter))  # Debug Print
    match_res.append(letter_hits)                  # Save count of the hits in names
    #print("{} tested with word {} result is {}".format(aname,aword,letter_hits))  # debug

#funky Love test rule combine the numbers as strings to makeup new number
match_score = int( str(match_res[0]) + str(match_res[1]) )

if match_score < 10 or match_score > 90:
    print("Your score is {}, you go together like coke and mentos.".format(match_score))
elif match_score < 50 and match_score > 40:
    print("Your score is {}, you are alright together.".format(match_score))
else:
    print("Your score is {}.".format(match_score))

Tanya and I score 54. I take it that is good, otherwise…

Welcome to the Love Calculator!
What is your name? 
Billy Baker
What is their name? 
Tanya Baker

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑