#100DaysOfCode - Day 3

if/elif/else and (For loop just by me)

·

5 min read

I have just finished day three of the #100DaysOfCode course I am working through.

The course has a number of small projects and one major project. For the major project, I can only say that I made minor changes to the code. I wasn't feeling too inspired to go beyond the remit of 'Make it your own'.

That said, I did do a lot with the small projects. Projects that worked through various methods of using if/elif/else statements and calculations.

The BMI calculator from the previous day was stepped up beyond the value as an output. Using if/elif/else to print to console a statement dependent on the value of the BMI. I used a function again, this time getting the input from the user and applying it to the calculation function as attributes.

def bmi_calculator(weight, height):
    bmi = round(weight / (height ** 2), 1)  # Last value is to the # of decmal places

A slight change in the calculation rounding down and setting the float output to one decimal place. Inserting the value into a f string statement.

if bmi < 18.5:
        print(f"Your BMI is {bmi}, you are underweight.")

Odd or Even A simple code using modulo 2 to determine if a number is odd or even.
The user inputs a value, it is converted to an integer and divide by two.
If the remainder is 1 a statement is console logged to say the value was odd and if the remainder is 0 there is a console log to state the value to be even.
I added in a try/except to ensure the value entered by the user was an integer or captured in a ValueError and restarted the code.

Leap Year calculation.
A user inputs a year and the code determines whether that year was a leap year or not. I thought the way the lesson focused on the use of the if/else nested methods but continued to use the modulo strange. Using the modulo method and only concerned with the value being equal to zero. Dividing the year by modulo 4 and getting the remainder 0, then if 0 doing the same for modulo 100 and finally by modulo 400 and then declaring it a leap year. My initial writing, before listening to the lesson and attempting the requirement was this...

def leap_year_check(user_input):
    checker = int(user_input % 4) 
    # Remember this is not a modulo of 2, there is more than 0 or 1 remainder

    if checker != 0:
        print(f"{user_input} is not a leap year.")
    else:
        print(f"{user_input} is a leap year.")

Using this method is still based against the remainder value of zero, and this is the value that determines a leap year whilst using a modulo 4. The other possible remainders 1, 2 & 3 all point to the year not being divisible by four and this is the only criteria for a leap year. This gives a much smaller code footprint and is a lot easier to read and understand what is happening. Other than giving unnecessary code, I can only assume it was a way to show nested if statements working, as both 100 % 400 are divisible by 4 giving a remainder of 0.

Pizza Order
This small project was more in line with the if/else nesting and it allowed me to again break out from the single block of code and use a set of functions.

  • Order the Pizza Size
  • Ask if they want to add a couple of extras
    The user selects the size of pizza, this then sets a value to the cost the user would pay.
    The cost and the size of pizza are passed to the second function. This function requires the size, as when the user requests the first extra, the cost of the extra depends on the size of the pizza. If selected, the additional cost is added to the cost of the order.
    A third function receives only the current cost of the order from the second function, as this function is for the second extra and the size of the pizza is not required to determine the cost of the pizza if the user wants to add this extra to the pizza. Again, if the user does request this extra, it's cost is added to the cost of the order.
    Finally, the user is presented with the cost of the order.
    Yes, this could have gone further but I am trying to stick to the 1-2 hours per day. I could have added would have been details of the order along with the cost.

Love Calculator
This is based on the children's game of counting the number of times the letters from 'True Love' appear both the name of your crush and your name.
The initial function requests the two names and passes them to a second function that does the calculation and returns that to the initial function. This value is sent onto a third function that, using an if/elif, gives a statement based on that value.
The initial is nothing new with the input passed onto other functions.
The second function uses a for loop to iterate each letter of the names against the string 'true love'. I converted the string 'true love' to set to remove the duplication of the letter 'e' as that would double the value of any name with the letter 'e' in it.
A set does not allow duplicate entries.

def love_calc(name1, name2):
    true_love = set("truelove")
    total_score_name1 = 0
    total_score_name2 = 0
    for x in true_love:
        name1_count = name1.count(x)
        total_score_name1 = total_score_name1 + name1_count
        name2_count = name2.count(x)
        total_score_name2 = total_score_name2 + name2_count

The other small projects in this set of lessons were of a similar nature. The input of values and running through a set of if/elif/else statements would determine the outcome. height and age determining whether the user can ride the rollercoaster and how much it would cost. Add in the extra cost of the snapshot memento at the end of the ride.
The major project, it was a text based rpg. A simple one but, as you can see from the above it would fit in well with the if/else nature of today's lessons. Turn left DIE! Turn right all good....