Week 5: Short Break & Python
I started the week by working on the "Apply Discount Function" lab. I was so confused at first because I couldn't use a parameter outside of the custom function then I figured I have to use conditionals inside the function. I did that but I also forgot how to check for the type. I incorrectly used the type() function but after some searching I remembered about the isinstance function. Here is how to use that isinstance function:
if not isinstance(price, (int or float)):
print('The price should be a number')
It checks if the parameter is an int or float before stating it isn't a number. At some point I realized I don't need to include the 'or' inside the parenthesis. I removed and replaced it with a comma. I couldn't figure out why my code still wasn't accepted so I searched around for an explanation and watched a few videos. Eventually I realized I had placed discount = discount/100 at the very beginning of the custom function so I moved it to the end. Here is my final code that passed:
def apply_discount(price,discount):
if not isinstance(price, (int, float)):
return 'The price should be a number'
elif not isinstance(discount, (int, float)):
return 'The discount should be a number'
elif price <= 0:
return 'The price should be greater than 0'
elif discount < 0 or discount > 100:
return 'The discount should be between 0 and 100'
else:
discount = discount/100
return price - (price * discount)
I learned a lot in that workshop. Then I moved on to 'Build a Caesar Cipher.' I've been wanting to decode a cipher! So building one will be a good start.
I completed the first five steps, but then I couldn't make time to continue. I was pretty confused when I started building cause I forgot about string slicing. I had to check my notes over and over to find what I was missing. Eventually it clicked so my notes were definitely helpful. Next week I'll finish out the Python basics and start the Network Services THM module. Hopefully I can also make the packet sniffer, but I might have to study a bit more Python.

