Captain's Log Progress Report
Anson learns Python
Here's a progress log on what Anson learns on his Python journey. Just to keep track and see progress.
Current Goals
These are taken from notes on 1/5/21
- Finish the Udemy course on Python
- Create the game snake
- Turn my Excel sheet of my video game collection into a SQLite3 database to learn more about Python and databases.
- Learn about Kivy and Android Studio
- Port snake to Android
- Figure out how to turn the SQLite3 video game collection database into an app so I can search through my collection on my phone.
- Port snake to pybadge and circuitpython
12/20/20 Sun
Starting! My 2 week goal is to create the game Snake in Python. So I can kickstart my new game dev studio.
Learned how Visual Studio Code ("VSCode"), git, and Jupyter notebook worked. Setup note taking log on Phil's Notes Bookstack (Thanks Phil). Created Jupyter notebook for local notetaking for Anson. Created alias to backup files to backup hard drives.
In the Udemy course, I learned:
- Data Types
- Numbers
- Variable Assignments
- Strings
- String Indexing
- String Slicing
12/21/20 Mon
Did some research to see what libraries were available for the PyBadge in CircuitPython. Found code that someone made to create snake in micropython. Wondering if that can translate to my pybadge.
In the course today, I learned about:
- String Properties
- Print formatting with strings
- Lists
12/22/20 Tues
Discovered these Markdown notes from the Jupyter Notebook documentation which were really helpful. And learned that VSCode can do Markdown editing with previews so I'll be using VSCode to edit all my Markdown notes now!
In the course I learned about:
- Dictionaries
- Tuples
- Sets
- Boooleans
- Manipulating files within Jupyter Notebooks and Python 3
Then took the 00-Python Object and Data Structure Basics assessment!
12/28/20 Mon
After a smol holiday break, I'm working on the Udemy course again. Today I learned about:
- Comparison/Boolean Operators in Python
- Chaining Comparison/Boolean Operators
- And
- Or
- Not
12/30/20 Wed
More Udemy coursework. Today I learned about:
- Control Flow - if/else/elif Statements
1/5/21 Tues
New year, same bullshit. Let's learn more Python. Last night I came up with some more goals and ideas:
-
My original goal stands: I want to finish the Udemy course and make the game snake still.
-
I want to learn more about Python and SQLite3 databases. SQLite3 should be a part of Python. So I want to load up my video game collection into a database.
-
I want to turn the database into an Android app using Kivy. So take that database and turn it into a way that I can access the info on my phone.
-
I want to port my snake game into Android as well. Porting the .py file into an .apk.
-
Poems for your sprog message of the day: the idea is that I'd create a python program/script that pulls all of /u/poem_for_your_sprog's poems and comments on reddit and displays them as a message of the day when you open up your terminal. Would also be really fun as a bot on discord to get a random sprog.
Ubuntu Machine - Installing Jupyter Notebooks and Python
I switched to my Ubuntu machine today. It didn't have pip, anaconda, jupyter, or anything that allowed the Jupyter notebooks to run Python. I kept getting "Data Science libraries notebook and jupyter are not installed in interpreter Python 3.8.5 64-bit." as an error. To fix this, I had to install pip, anaconda, ipykernel, and jupyter. Anaconda came as a script from the anaconda website. To install scripts, I just dragged the script file in the GUI into the terminal. Then I ran the these terminal commands. Remember updateme is my alias that does sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt autoclean -y && sudo snap refresh
.
updateme
python3 -m pip install --upgrade pip
python -m pip install ipykernel --force
python -m pip install jupyter --force
In terms of the Udemy course, today I learned about:
- For Loops
1/6/21 Wed
GDQ and trying to learn Python is hard but let's make a little progress today!
In terms of the Udemy course, today I learned about:
- While Loops
- Keywords for Loops - Break, Continue, and Pass
- Range
- Enumerate
- Zip
- In
- Min/Max
- Random Library
- Input
- List Comprehension
Temperature Converter Program
AfterSee I got to the end of the Udemy course and it was testing time, I tried to write a program that converts temperature into Celsius and Fahrenheit. I used this code as the starting point.
Pieces of code I'll need to understand the notes going forward:
Temperature Converter Baby Code.py - For base baby codeTemp Convert Phil 1.py - For Step 1 codeTemp Convert Phil 2.py - For Step 2 codeTemp Convert Phil 2 - Anson Mod Final.py - For Step 2 code - Final Code
My base baby code
So here's the basic code I made using the starting point. I added in the temperature rounding and the Unit checking.
# Intro Words
# This is a beginner programlink for Anson to learn how to make a simple
# temperature conversion program. This will convert from C to F and in reverse
temp = float(input("Enter temperature = "))
unit = input("Enter C or F (for Celsius or Farhenheit) = ")
if unit == "F" or unit == "f":
fahrenheit = (temp * 9/5) + 32
print(f'{round(fahrenheit,2)} F')
elif unit == "C" or unit == "c":
celsius = (temp - 32) * 5/9
print(f'{round(celsius,2)} C')
elif unit != "F" or unit != "f" or unit != "C" or unit != "c":
print("Not a valid unit, try again.")
Next steps
I want to have the code check for errors. So if I put in temp = asf and unit = F, I want it to tell me I'm bad.I want the code to loop through 3 times. So if I make mistakes, I want it to re-prompt me again to input my values. After 3 guesses, it quits out.
Step 1 Code
The base code is "Temperature Converter Baby Code.py". Then I wanted to add in mistake proofing so if I put in asdf as the temperature, it would say "Oh you messed up, try again" and reprompt me. Jwaz nerd chat introduced me to "try/except" which is explained nicely here. And they also introduced me to NameError and ValueError which is explained here.
Try/Except doesn't won't set anything if it doesn't work though. It only sets the variable if it works. So I'm getting a NameError with this code I came up with for Step 1 (I got the idea for checking if the value is a float from here):
# Intro Words
# This is a beginner program for Anson to learn how to make a simple
# temperature conversion program. This will convert from C to F and in reverse
temp = input("Enter temperature = ")
unit = input("Enter C or F (for Celsius or Farhenheit) = ")
try:
temp_float = float(temp)
except ValueError:
print("Not a valid temp")
while temp_float == float:
if unit == "F" or unit == "f":
fahrenheit = (temp_float * 9/5) + 32
print(f'{round(fahrenheit,2)} F')
elif unit == "C" or unit == "c":
celsius = (temp_float - 32) * 5/9
print(f'{round(celsius,2)} C')
elif unit != "F" or unit != "f" or unit != "C" or unit != "c":
print("Not a valid unit. Try again.")
if temp_float != float:
print("You messed up. Try again.")
Phil helped me with the code "Temp Convert Phil.py". His code looks like:
temp = (input("Enter temperature = "))
unit = input("Enter C or F (for Celsius or Farhenheit) = ")
try:
temp_float = float(temp)
except ValueError:
print("Not a valid temp")
quit()
if not temp_float:
print("You messed up. Try again.")
quit()
if unit.lower() == "f":
fahrenheit = (temp_float * 9/5) + 32
print(f'{round(fahrenheit,2)} F')
elif unit.lower() == "c":
celsius = (temp_float - 32) * 5/9
print(f'{round(celsius,2)} C')
else:
print("You messed up. Try again.")
Phil's notes on the code:
He taught me thatquit()orexit()are things that exist. So now the code just quits out when I put in the wrong values.A while loop I would reach for if I hadmorethan one thing to iterate through, but in this situation we have 1 unique thing to make a decision on 1 time. I might use a while if I had 1000 temperatures but I couldn't be sure how many times I needed to go through the processing for conversionThe if elif else is the right way to think abou tthe linear decision process, if one thing, otherwise if another thing, otherwise here's what to do when you're out of optionsFinally, because in my solution I didn't use a loop of any kind, pass doesn't work for this either. In a loop a pass would be proper if you needed a reason to exit the loop but continue to the logic after the loop. Instead, when you need to exit the whole program, exit() or quit() would be better tools
information
Step 2 Code
Now I want the code to repeat itself. I want the code to allow me 3 tries before quitting out. I had the ideal of using true or false booleans and while loops but wasn't sure how to bridge the gap. Phil helped with this code called "Temp Convert Phil 2.py":
# Instantiate initial true flag to enter loop
run_loop = True
# While set to go
while run_loop:
# Try to capture a float at input time so we don't have to parse it later
try:
temp = (float(input("Enter temperature = ")))
except Exception:
print("Input isn't a temperature; try again")
# If we can't establish a float for the first input, we'll simply skip the rest of this iteration and never set
# the run_loop flag to false, allowing loop to continue
# Exception catches all errors
# more info here: https://docs.python.org/3/library/exceptions.html
continue
# Instantiate a sub loop
run_loop_sub = True
while run_loop_sub:
try:
unit = str(input("Enter C or F (for Celsius or Farhenheit) = "))
except Exception:
print("Input needs to be c/C or f/F")
# Harder to hit this since "" is a string in input, but if it fails for whatever reason
# just try again
continue
if unit.lower() == "f":
fahrenheit = (temp * 9/5) + 32
print(f'{round(fahrenheit,2)} C')
# Completion condition met, set loop flag to false to exit loop after this iteration
run_loop_sub = False
elif unit.lower() == "c":
celsius = (temp - 32) * 5/9
print(f'{round(celsius,2)} F')
# Completion condition met, set loop flag to false to exit loop after this iteration
run_loop_sub= False
else:
print("You need to enter either c/C or f/F")
# There is no satisfactory completion here, so don't set the close flag
# If we make it here, that means that the sub while loop was satisfied, and there is no further exceptions to
# skip this flag; we can probably end the loop
run_loop = False
Notes on the code:
Instantiateto represent an instanceTry/Except has theExceptionclass which catches all built-in, non-system existing exceptions. This is what I wanted when I was messing withValueErrorandNameErrorabove.Instead of the!=I was using to check for the case, the way Phil handled it was to just parse the input into a string then usesunit.lower()to set the case to lower case only. That way it doesn't matter what the case is.Comments are goodLoops and subloops are a thing. Could have a overall loop and loops underneath the overall loop
So now I got most of the code working. "Temp Convert Phil 2.py" aka the code above will keep looping until it gets the correct answer. But now I want to add in the 3 strikes or the code ends. So my code is found in "Temp Convert Phil 2 - Anson Mod Final.py". It looks like:
# Instantiate initial true flag to enter loop
run_loop = True
# Set global variable to count
retry = 0
# Runs code while retry is under 3
while retry < 3:
# While set to go
while run_loop:
# Try to capture a float at input time so we don't have to parse it later
try:
temp = (float(input("Enter temperature = ")))
except Exception:
# Exception catches all errors
# more info here: https://docs.python.org/3/library/exceptions.html
print("Input isn't a temperature; try again. Max 3 attempts. Attempts:",retry+1)
retry += 1
# If we can't establish a float for the first input, we'll simply skip the rest of this iteration and never set
# the run_loop flag to false, allowing loop to continue
# Print is setup so it tells me how many attempts I'm at and shows the count
break
# Ends the program if I guess too much
# Instantiate a sub loop
run_loop_sub = True
while run_loop_sub:
try:
unit = str(input("Enter C or F (for Celsius or Farhenheit) = "))
except Exception:
print("Input needs to be c/C or f/F")
# Harder to hit this since "" is a string in input, but if it fails for whatever reason
# just try again
continue
if unit.lower() == "f":
fahrenheit = (temp * 9/5) + 32
print(f'{round(fahrenheit,2)} C')
# Completion condition met, set loop flag to false to exit loop after this iteration
run_loop_sub = False
elif unit.lower() == "c":
celsius = (temp - 32) * 5/9
print(f'{round(celsius,2)} F')
# Completion condition met, set loop flag to false to exit loop after this iteration
run_loop_sub= False
else:
print("You need to enter either c/C or f/F")
# There is no satisfactory completion here, so don't set the close flag
# If we make it here, that means that the sub while loop was satisfied, and there is no further exceptions to
# skip this flag; we can probably end the loop
print('You know the temp now!')
run_loop = False
#if retry == 3:
# run_loop = False
# Not sure if this helps or hurts
# After experimenting, it doesn't seem to matter if it's here
Notes on the code:
There's a hang up, the code doesnt close cleanly like it did in "Temp Convert Phil 2.py" after the temperature is input correct. But it does work.When the 3 guesses are put in, so "asdf" for the temperature, I created a print that warns how many attmepts are left and what attempt number we're on.The last 4 lines of code don't seem to do much. Not sure why I can just run a while loop as is.Learned what a global variable is.Learnedwhat a counter isand how break works.