Upgrading Basic Python Scripts
Published: 2023-01-31 18:50:39

---
layout: post
title: Upgrading Basic Python Scripts
---

So, I'm no python engineer, but I'm kinda good at it. So here goes nothing. I will be upgrading 1 basic script and from the Programming With Mosh begginers
python in 1 hour tutorial (and one bonus), explaining them, and then upgrading them.
BTW, for reference I am using PyCharm Community to write these. Finally the upgraded versions and non upgraded versions will be saved on my Github (DiamondBroPlayz)


Here is Programming With Mosh's version:

birth_year = input("Enter your birth year: ")
age = 2022 - int(birth_year)
print(age)

Pretty simple to understand, takes use input at the top, then makes a variable to store a math equations output, and prints that variable, or if you didn't
understand that then here's it with comments (courtesy of me):

# Storing birth year
birth_year = input("Enter your birth year: ")

# Doing the math equation
age = 2022 - int(birth_year)

#Outputing the results
print(age)

If you still can't understand it, then this isn't the blog for you. This is a coding focused blog.

Now to upgrade it, my first idea was just checking the exact year since it in its current state it just prays that it's 2022. But when I looked farther into my
"Upgraded" code I just found more and more stuff I could improve, so here it is: (NOTE: [The github repo](https://github.com/DiamondBroPlayz/upgraded-python-beginner-scripts/) includes both versions)

I'll show the code side to side and then explain it.

NEW (My upgrading of my upgrading)                                                                           OLD (Mosh's unmodified (Besides the year) version)
try:                                                                                                         birth_year = input('What is your birth year? ')                                                                                                        birth_year = input("Enter your birth year: ")
  import datetime                                                                                            age = 2022 - int(birth_year)
except ImportError:                                                                                          print(age)
  import subprocess
  subprocess.check_call(['python', '-m', 'pip', 'install', 'datetime'])

# Fullfill requirenments

from datetime import date

# Variables

today = date.today()
year = today.year

# Start

# Asking for user input
while True:
  try:
    birth_year = int(input('What is your birth year? '))
    if birth_year > year:
      print("Your birth year cannot be in the future.")
    elif birthyear < 1900:
      print("Your birth year is too far in the past.")
      continue
    break
   except ValueError:
    print('Please input a valid birth year.')
    
# Doing the math:
age = year - birth_year

#Printing results
if age == 0:
  print('You are 0 years old today! (That is definentley a fake age)')
else:
  print(f"You are {age} years old today.")

As you can see, my version is signifigantly better! I had a bit of help from ChatGPT, but I did a lot of it myself.
The new features:
  Year-checking
  Automatically checking for "datetime" (needed because of year-checking) and installing if it's not already installed
  Checking if the year is valid with a range
  More that I don't wanna list
Now, of course this is way larger, but if you needed this for some reason then it's good! Now the explanation, I can't plausably fit it in this. But, it will be
included in [the github repo](https://github.com/DiamondBroPlayz/upgraded-python-beginner-scripts/)

That's it! There's some more in the github repo.
Github: DiamondBroPlayz
Youtube: DiamondBroPlayz