codingstreets
Search
Close this search box.

Python Conditional Statements (if else) Project for Beginners

Python-Conditional-Statements-if-else-Project-for-Beginners
Credit: Canva

Overview

Python if else project is designed to challenge yourself to solve a couple of Python beginners projects, and see where you stand! If else in Python is known as a conditional statement and is used to check whether the given statement is matched or not with any of the conditions.

What’s Next!

In this Python Conditional Statements beginner project, we will go through a couple of Python if else projects to enhance the solving ability as well as go with a code of line. Let’s solve it while having fun too.

Table of Contents

Project-1: Greetings on time

Write the code to greet Good Morning, Good Afternoon, Good Evening according to the time.

				
					# time frame: Good Morning - 5 AM to 11 AM
# time frame: Good Afternoon - 12 PM to 17 PM
# time frame: Good Evening - 18 PM to 23 PM

import time

Hour = int(time.strftime("%H"))

if Hour >= 5 and Hour <= 11:
    print("Current time is:", time.strftime("%H:%M:%S"), " ->Good Morning.")
elif Hour >= 12 and Hour <= 17:
    print("Current time is:", time.strftime("%H:%M:%S"), "->Good Afternoon.")
elif Hour >= 18 and Hour <= 23:
    print("Current time is:", time.strftime("%H:%M:%S"), "->Good Evening.")

else:
    print("Current time is:", time.strftime("%H:%M:%S"), "->It is sleeping time.")

				
			

Explanation: From the time module, we used the function strftime (string format time) that returns the current time in string format. Here code H refers to hours, M for Minutes, and S for Seconds.

Since the program first compares the time and then executes the print statement, therefore it is necessary to convert the strftime type() from str to int so that the program does not get an error of TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’, can compare the strftime with if..else statements to execute the print statement.

For more details: Python datetime

Project-2: Grade Calculator

Write a program to return that takes a student’s grade as an input returns grade based on the marks lie in given criteria: A for scores >= 90, B for scores between 80 and 89, C for scores between 70 and 79, D for scores between 60 and 69, and F for scores below 60.

				
					"""
1-59=F
60-69=D
70-79=C
80-89=B
90 and over-A

"""

student_score = int(input("Enter the student's score:" ))
if student_score  >= 90:
    print("The grade is A")
elif student_score >= 80 and  student_score <= 89:
    print( "The grade is B")
elif student_score >= 70 and  student_score <= 79:
    print( "The grade is C")
elif student_score >= 60 and  student_score <= 69:
    print("The grade is D")
else:
    print("The grade is F")
				
			

Explanation: The program starts with asking the student’s score as an input. As per the if..else statements, the student’s score is compared with the given criteria and returns the print statement that matches the criteria.

Project-3: Temperature Converter

Write a program that converts temperatures between Celsius and Fahrenheit. The program should ask the user to input a temperature along with the unit (C or F) and then convert it to the other unit.

				
					temperature = int(input("Enter temperature: "))
choice = input("C for Celsius and F for Fahrenheit")

if choice == "C":
    Fahrenheit = (temperature * 9/5) +  32
    print("%.2f" %temperature, "degrees Celsius =", "%.2f" %Fahrenheit, "degrees Fahrenheit.")
    
    #uncomment the following line, if want to remove zero from parameter
    #print("%d" %temperature, "degrees Celsius =", "%d" %Fahrenheit, "degrees Fahrenheit.")
    
elif choice == "F":
    Celsius = (temperature - 32)  * 9/5
    print("%.2f" %Celsius, "Degrees Celsius")
else:
    print("Invalid Input! Please enter C or F.")

				
			

Explanation: The program starts with taking temperature as input and next asks the user to choose the type of temperature by selecting “C for Celsius and F for Fahrenheit”.

Now, according to the conditional statements (if…else), the user’s input will be checked on the given criteria and returns the output based on the criteria matched.

Note: “%.2f” – “%f” is a format specifier to convert the integer from int to str type. And “.2” denotes the number of zeros after a decimal.

Project-4: Number Guessing Game

Write a program to develop a number guessing game. The program starts with picking a random number between 1 and 100; the user must guess the number. The program should provide feedback to the user (too high, too low, or correct) and keep track of the number of guesses.

(Optional)

When you create the above program, let’s add a little bit of fun! Limit the number of attempts a user can guess the number. In this case, the program is limited to 3 guesses only and notifies the user with each guessing how many guesses are left.

				
					import random

print("You have total 3 Chances")
number_of_attempts = 3
while number_of_attempts > 0:
    number_of_attempts  -=1
    random_number =  random.randint(1, 101)
    guess_number = int(input("Guess a number: "))
    print("User's guess:",guess_number)

    print("Random Integer:",random_number)
    
    if guess_number == random_number:
        print("User guess is correct.")
    elif  guess_number > random_number:
        print("User guess is too high.")
    else:
        print("User guess is too low.")
    
    while number_of_attempts == 0:
        break
    else:
        print("You have left only", number_of_attempts,"attempts")
print("You have used up all your attempts")

				
			

Explanation: The program starts with guessing a number by the user and with each guessing it is counted by using a while loop. Next, a random number is generated by the computer by using the randint() function.

Now, the user’s and computer’s numbers are compared with conditional statements, and based on the criteria print statement is executed.

Project-5: BMI Calculator

Write a program that calculates the Body Mass Index (BMI) based on a person’s height and weight. The program should ask the user to input their height (in inches or centimeters) and weight (in pounds or kilograms) and then calculate and display their BMI along with the corresponding category (underweight, normal, overweight, or obese).

				
					weight = int(input("Enter your weight in KG:", ))
height = int(input("Enter your height in CM:", ))

print("User's Weight:", weight)
print("User's Height:", height)

BMI_formula = (weight/(height*height))*10000
BMI = BMI_formula
print("BMI: ", BMI)

if  BMI < 18.4:
    print("You're Underweight.")
elif BMI == 18.4 or BMI < 24.9:
    print("You're Normal.")
elif BMI == 24.9 or BMI <=39.9:
    print("You're Overweight.")
else:
    print("You're Obese.")
				
			

Explanation: The program starts with taking the user’s input as weight and height. Next, BMI is calculated by using the BMI’s formula, and based on the BMI, the print statement is executed according to the condition matched in conditional statements  (if else).

Recent Articles