CodeNewbie Community 🌱

Vishnubhotla Bharadwaj
Vishnubhotla Bharadwaj

Posted on • Originally published at bharadwaj.hashnode.dev on

Day-3, 100 days of Python

On the third day, I learned about Conditional statements, Logical operators, Code blocks, and Scope. All together at the end of the day, we can develop our own adventure game.

Control flow with if-else and conditional operators:

if condition:
      do this (give a tab distance as it is an indentation)
else:
      do this

Enter fullscreen mode Exit fullscreen mode

Comparison operators are : >, <, >=, <=, ==(checking equality), !=(not equals)

Simple Even or Odd number program using above concepts:

a = int(input("Enter number to check"))
if a%2==0:
    print("even")
else:
    print("odd")

Enter fullscreen mode Exit fullscreen mode

Ok!! Now a simple height check program for a roller-coaster ride

print("Welcome to roller-coaster")
a = int(input("Enter height in cms: "))
if a>120:
    print("Give him ticket")
else:
    print("not allowed")

Enter fullscreen mode Exit fullscreen mode

If we are using the if-elif-else format then statements are checked orderly and if the statement satisfies then it skips other statements and produces only one output corresponding to the correct statement.

Contrary to that if we are using if-if... this is like nested if (if inside if) then every statement will be checked, executed and if the statements satisfy then corresponding outputs are printed.

To demonstrate each of the above statements, here is the code attached

Using if-elif-else :

height = input("enter your height in m :")
weight = input("enter your weight in kg :")
#bmi = weight/(height*height)
#print(type(height))
a = float(weight)
b = float(height)
bmi = round(a/(b*b),2)
if bmi<18.5:
    print(f"your bmi is {bmi}, you are under weight")
elif bmi>=18.5 and bmi<25:
    print(f"your bmi is {bmi}, you are normal weight")
elif bmi>=25 and bmi<30:
    print(f"your bmi is {bmi}, you are over weight")
elif bmi>=30 and bmi<35:
    print(f"your bmi is {bmi}, you are obese")
else:
    print(f"your bmi is {bmi}, you are clinically obese")

Enter fullscreen mode Exit fullscreen mode

Using nested if :

print("Welcome to roller-coaster")
a = int(input("Enter height in cms: "))
age = int(input("Enter your age"))
if a>120:
    print("Give ticket")
    if age>18:
        print("Pay 12 dollars")
    else:
        print("pay 7 dollars")
else:
    print("not allowed")

Enter fullscreen mode Exit fullscreen mode

Using both the formats in a combined way :

print("Welcome to roller-coaster")
a = int(input("Enter height in cms: "))
age = int(input("Enter your age"))
if a>120:
    print("Give ticket")
    if age>18:
        print("Pay 12 dollars")
    elif age>12 and age<=18:
        print("pay 7 dollars")
    else:
        print("pay 5 dollars")
else:
    print("not allowed")

Enter fullscreen mode Exit fullscreen mode

Checking the given year a leap year or not :

Steps to be followed:

  1. If it is divisible by 4 then it is a leap year, else not.
  2. If it is divisible by 100 then it is not a leap year, else it is.
  3. If it is divisible by 400 it is a leap year, else not.

All three steps must be combined to write the code for leap year.

Code for leap year is as follows:

a = int(input("Enter year to check: "))
if a%4==0:
    if a%100==0:
        if a%400==0:
            print("Leap year")
        else:
            print("Not a leap year")
    else:
        print("leap year")
else:
    print("Not a leap year")

Enter fullscreen mode Exit fullscreen mode

Now, adding a little feather to our roller-coaster program that is taking the input after certain statements and including it in program:

print("Welcome to roller-coaster")
a = int(input("Enter height in cms: "))
age = int(input("Enter your age"))
bill = 0
if a>120:
    if age>18:
        bill = 12
        print("Adult ticket is 12 dollars")
    elif age>12 and age<=18:
        bill = 7
        print("youth ticket is 7 dollars")
    else:
        bill = 5
        print("Child ticket is 5 dollars")

    p = input("Do you want photo? Y or N")
    if p=="Y":
        bill+=3
    print(f"Total bill is{bill}")
else:
    print("not allowed")

Enter fullscreen mode Exit fullscreen mode

Two conditions can be combined by using "and" and "or". If "and" is used then both the conditions must be satisfied to produce output. But in the case of "or" if one condition is correct then we can get our output.

Program using "and":

print("Welcome to pizza deliveries")
s = input("Enter the size of the pizza? S, M, or L")
p = input("Do you need pepper included? Y or N")
c = input("Do you need cheese included? Y or N")
b = 0
if True:
    if s=="S":
        b = 15
        print("Size of small pizza is: $15")
    elif s == "M":
        b = 20
        print("Size of medium pizza is: $20")
    elif s == "L":
        b = 25
        print("Size of large pizza is: $25")
    else:
        print("Enter correct pizza size.!!")
    if p=="Y" and s=="S":
        b+=2
    elif p=="Y" and s=="M":
        b+=3
    elif p=="Y" and s=="L":
        b+=3
    elif p=="N":
        print("No pepper No issues")
    else:
        print("Enter crct pepper value")
    if c=="Y":
        b+=1
print(f"your final bill is: {b}")

Enter fullscreen mode Exit fullscreen mode

.lower() function converts string into lower case. and .count() function counts the required alphabet number (like how many times it is repeated).

A love calculator program (looks to be weird but ok) using both the above functions and the concepts learned earlier:

print("Welcome to calc")
a = input("Enter your name")
b = input("Enter their name")
c = a.lower()
print(c)
d = b.lower()
print(d)
e = c.count("t")+d.count("t")
f = c.count("r")+d.count("r")
g = c.count("u")+d.count("u")
h = c.count("e")+d.count("e")
m=e+f+g+h
i = c.count("l")+d.count("l")
j = c.count("o")+d.count("o")
k = c.count("v")+d.count("v")
l = c.count("e")+d.count("e")
n = i+j+k+l
o = str(m)+str(n)
print(f"Percentage of love is {o}")
p = int(o)
if p<=10 or p>=90:
    print(f"Your score is {o}, you go together like coke and mentos")
elif p>=40 and p<=50:
    print(f"Your score is {o}, you are alright together")
else:
    print(f"Your score is {o}")

Enter fullscreen mode Exit fullscreen mode

Ok!! That's the warp of day-3 and the final project of the day building our adventure game is here:

print("Welcome to treasure island")
print("Your mission is to find the treasure")
a = input("Now you are at the crossroad? select 'Left' or 'Right'")
if a == "Left":
    print("Now you are in front of the lake!!")
    b = input("Select one? Wait for boat or Swim")
    if b == "Wait":
        print("Now you have reached the other end of the shore!")
        print("You have three doors, select one of them")
        c = input("Red, Yellow, or Blue")
        if c == "Red":
            print("Game over!! as the room is full of snakes")
        elif c == "Yellow":
            print("You have won!!")
            print("Entire treasure is yours")
        else:
            print("Game over!! as the room is full of beasts.")
    else:
        print("Game over!! as the river is full of crocodiles")
else:
    print("Game over")
    print("You have accidentally fallen into hole")

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
alotubvid profile image
alotubvid

On Day 3 of the 100 Days of Python challenge, you'll continue to build your programming skills by working on practical exercises and projects. As you progress, consider applying your knowledge to real-world problems, such as developing tools or applications for the management of chronic illness. This can include creating data tracking systems or health management apps that support individuals in monitoring and managing their chronic conditions more effectively.