# File: if2.py # Program to demonstrate various forms of the "if" statement # # Matt Bishop, MHI 289I, Fall 2024 # # # This shows the "if" statement in various forms # # n = int(input("Enter a 0 or a 1 or a 2 or a 3: ")) # # first, the "if" # print("First, the if") if n == 0: print("You typed a 0!") # # now, the "if-else" # print("Now the if ... else") if n == 0: print("You typed a 0!") else: print("You didn't type a 0") # # now, the "if-elif-else" # print("Now the if ... elif ... else") if n == 0: print("You typed a 0!") elif n == 1: print("You typed a 1!") elif n == 2: print("You typed a 2!") else: print("You didn't type a 0 or a 1 or a 2") # # now, the "nested if" # print("Now, the nested if") if n > 1: print("You typed something greater than 1!") if n < 3: print("You typed something greater than 1 and less than 3 -- that is, 2") else: print("You typed something greater than 1 and not less than 3 -- that is, 3 or more") else: print("You typed something not greater than 1!") if n < 1: print("You typed something not greater than 1 and less than 1 -- that is, 0 or less") else: print("You typed something not greater than 1 and not less than 1 -- that is, 1") # # Now comparing strings # pwd = input("Now type your password: ") if pwd == 'abcde': print("Log in successful . . . if this were a real login") elif pwd == 'fghij': print("Log in may have worked") else: print("Log in failed!")