0% found this document useful (0 votes)
3 views9 pages

If Else

The document contains a series of Python code snippets demonstrating the use of conditional statements (if, else, elif) and nested conditions. Each snippet prompts the user for input and performs various checks, such as age verification, number comparisons, and character evaluations, with corresponding outputs. The examples illustrate basic programming concepts and control flow in Python.

Uploaded by

cslabador6830val
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

If Else

The document contains a series of Python code snippets demonstrating the use of conditional statements (if, else, elif) and nested conditions. Each snippet prompts the user for input and performs various checks, such as age verification, number comparisons, and character evaluations, with corresponding outputs. The examples illustrate basic programming concepts and control flow in Python.

Uploaded by

cslabador6830val
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

IF 1

#1
age = int(input('Enter your age: '))

if age >= 18:


print(age)
print("adult")
else:
print(age)

#2
first = int(input("Enter first integer: "))
second = int(input("Enter second integer: "))

if (first == second):
print(f"{first} {second}")
print("equal")
else:
print(f"{first} {second}")

#3

num1 = float(input("Enter first decimal number: "))


num2 = float(input("Enter second decimal number: "))

if (num1 > num2):


print(f"{num1:.2f} {num2:.2f}")
print("The first one is greater than the second one")
else:
print(f"{num1:.2f} {num2:.2f}")

#4
vowel = input("Enter a single character: ")

if vowel.lower() in 'aeiou':
print(vowel)
print("vowel")

else:
print(vowel)
#5

num1 = int(input("Enter a single integer: "))


if (num1 == 0):
print(num1)
elif (num1 % 2 == 0):
print(num1)
print("even")

else:
print(num1)

#6

first_decimal = float(input("Enter first decimal number: "))


second_decimal = float(input("Enter second decimal number: "))
third_decimal = float(input("Enter third decimal number: "))

print(f"{first_decimal:.2f} {second_decimal:.2f}", end=' ')

if third_decimal > (first_decimal + second_decimal):


print(f"{third_decimal:.2f}")
else:
print()

IF ELSE

#1
age = int(input("Enter age: "))

if age <= 17:


print("minor")
else:
print("adult")

#2
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))

num1 = a + b

if num1 > c:
print(f"{a} + {b} > {c} = yes")
else:
print(f"{a} + {b} > {c} = no")

#3
total = 50

a = int(input("Enter first positive integer: "))


b = int(input("Enter second positive integer: "))

if a % 2 == 0:
total += a
else:
total -= a

if b % 2 == 0:
total += b
else:
total -= b

print(total)

#4
num1 = int(input("Enter your birth year: "))

num2 = 2022 - num1

if num2 >= 60:


print("senior citizen")
else:
print("not senior citizen")

#5
character = input("Enter a character: ")

if character.lower() == 'y':
print("Here's your ice cream")
else:
print("Okay, maybe some other time")
#6
x = float(input("Enter x: "))
y = float(input("Enter y: "))
z = float(input("Enter z: "))

if x < y:
within_range = x <= z <= y
else:
within_range = y <= z <= x

if within_range:
print(f"{z:.2f} is within {x:.2f} and {y:.2f}")
else:
print(f"{z:.2f} is outside {x:.2f} and {y:.2f}")

IF ELSE IF ELSE

#1
num1 = int(input("Enter an integer: "))

if num1 == 0:
print("Zero")
elif num1 %2 == 0:
print("Even")
elif num1 %2 != 0:
print("Odd")

#2
character = input("Enter a character: ")

if character.lower() == 'm':
print("Male")
elif character.lower() == 'f':
print("Female")
else:
print("Prefer not to say")
#3
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

num3 = num1 + num2

if num3 >= 1:
print("Positive")
elif num3 <= -1:
print("Negative")
else:
print("Zero")

#4
val1 = float(input("Enter decimal value 1: "))
val2 = float(input("Enter decimal value 2: "))
val3 = float(input("Enter decimal value 3: "))

val4 = val1 + val2 + val3

if val4 < 200000:


print("average")
elif val4 >= 200000 and val4 < 400000:
print("rich")
elif val4 >= 400000 and val4 < 600000:
print("super rich")
elif val4 > 600000:
print("crazy rich")

#5
num = int(input("Enter an integer: "))

if num % 7 == 0 and num % 3 == 0:


print("CodeChum")
elif num % 7 == 0:
print("Code")
elif num % 3 == 0:
print("Chum")
else:
print("None of the above")
#6
num1 = int(input("Enter integer x: "))
num2 = int(input("Enter integer y: "))

if num1 > num2:


print("x is greater than y")
elif num1 < num2:
print("x is lesser than y")
elif num1 == num2:
print("x is equal to y")

NESTED

#1

years = int(input("Enter years of work: "))


work = input("Enter kind of work: ")

if years < 2:
if work.lower() == 'b':
print("Salary: 10,000")
else:
print("Salary: 20,000")
elif years < 5:
if work.lower() == 'b':
print("Salary: 12,000")
else:
print("Salary: 40,000")

elif years >= 5:


if work.lower() == 'b':
print("Salary: 15,000")
else:
print("Salary: 75,000")
#2

money = int(input("Enter current pocket money: "))


money_str = str(money)
if money_str[-1] == '7':
print(money)
print("Such lucky money!")
elif money <= -1:
print("Stop lying, tell me what's in your pocket!")
else:
print(money)
print("Just normal money.")

#3
name = input("Enter name: ")
last = name[-1].lower()

if last in 'aeiou':
age = int(input("Enter age: "))
if age % 2 == 0:
print("Wow, you're special!")
else:
birth = int(input("Enter birth year: "))
if birth % 2 == 0:
print("Oh, you're still special!")
else:
print("You will be special next year.")
else:
print("You're awesome!")
#4
total = 10
num1 = int(input("Enter an integer: "))

squared = num1 ** 2 + total


cubed = num1 ** 3 + total

if num1 % 2 == 0 and num1 > 1:


print(squared)
elif num1 % 2 != 0 and num1 > 1:
print(cubed)
elif num1 <= -1:
num2 = int(input("Enter another integer: "))
expo = num1 ** num2 + total
print(expo)
else:
print("Nothing happened.")

#5
lang_choice = int(input("Choose the programming language you like best: "))

if lang_choice == 1:
print("You like C best")
elif lang_choice == 2:
print("You like C++ best")
elif lang_choice == 3:
print("You like Java best")
elif lang_choice == 4:
print("You like C# best")
elif lang_choice == 5:
print("You like Python best")
dev = input("Enter type of development: ")
if dev.lower() == 'a':
print("Web")
elif dev.lower() == 'b':
print("Data Analytics")
elif dev.lower() == 'c':
print("Backend")
frame = input("Do you know any frameworks? ")
if frame.lower() == 'y':
print("Wow, you're good!")
#6

def determine_position(x, y, z):


"""Determines the position of z relative to x and y."""

if x <= z <= y:
print(f"{z} is just within {x} and {y}")
elif z < x:
print(f"{z} is farther from {y}") # Corrected based on samples
else:
print(f"{z} is farther from {x}")

# Get input values


x = int(input("Enter x: "))
y = int(input("Enter y: "))
z = int(input("Enter z: "))

# Call the function to determine position


determine_position(x, y, z)

You might also like