Skip to main content

Command Palette

Search for a command to run...

If-else Practice Questions

Updated
3 min read
If-else Practice Questions
C
I am Data Science student, has a little bit knowledge on Web Development. I also love writing and editing as my hobby. Passionate to explore the world.

Q No. 01: Create odd and even number program using if else statements.

Information:

If input = 40 so, Output = 0 (means number is even)

Logic for even number: ui%2 == 0

If input = 55 so, Output = 1 (means number is odd)

Logic for odd number: ui%2 == 1

ui = int(input("Enter any number"))  #if input = 600

if ui % 2 == 0:
    print(f"{ui} This is Even")
else:
    print(f"{ui} This number is Odd!")

Output:

If input = 600

600 This is Even

Q No. 02: Create a program of Grading System using elif statements.

Information:

Grading SystemGrading System
per >= 80 "A+"per >=40 "D"
per >= 70 "A"per >=33 "E"
per >= 60 "B"per <33 "Fail"
per >=50 "C"
per = 70

if per>=80:
    print("A+")
elif per >= 70:
    print("A")
elif per>= 60:
    print("B")
elif per>=50:
    print("C")
elif per>40:
    print("D")
elif per>=33:
    print("E")
else:
    print("Fail")

Output: A

Program of Grading System using elif and 'and' statement:

per = 99

if per>=0 and per<33: #step1
    print("Fail")
elif per >= 33 and per<40: #step2
    print("E")
elif per>= 40 and per <50:#step3
    print("D")
elif per>=50 and per<60:#step4
    print("C")
elif per>60 and per<70:
    print("B")
elif per>=70 and per<80:
    print("A")
else:
    print("A+")

Output: A+


Q No. 03: Create username and password system using nested if else statements.

username = input("Enter user name: \t")

if username == "admin":  # step1 only check username 
    print("Valid user")

    password = input("Enter your password: \t")
    if password == 'admin123': # step2  
        print("Valid user name and password!")
        otp = input("OTP")
        if otp == '123':# step3
            print("Welcome User")
        else:
            print("NotValid OTP blocked now!")
    else:
        print("re enter valid passwordadmin")

else:
    print("Please enter valid user name")

Output:

If input: username = admin and password = admin123 and otp =123

Valid user
Valid user name and password!
Welcome User

If input: username = abc (so it doenot move further towards password and otp, because username is wrong)

Please enter valid user name

If input: username = admin and password = 123 (so it doenot move further towards otp, because password is wrong)

Valid user
re enter valid passwordadmin

If input: username = admin and password = 123 and otp =xyz (so it shows NotValid OTP blocked now!, because otp is wrong)

Valid user name and password!
NotValid OTP blocked now!

SHORT METHOD:

Create User Login Password System using 'and' keyword along with if else loop

user = 'admin'
password = 'admin123'
otp = '123'

if user=='admin' and password == 'admin123' and otp == '123':# one layer
    print("Valid user")
else:
    print("Not valid user")

When all information written accurately then output will be 'Valid user'

Output: Valid user