#John Bible 11/22/21
#What I learned was how to use compound conditionals, boolean expressions, boolean operators, and how to combine comparisons.
#What I had problems with was part two of task two as I was unsure how to get the code to work but I overcame this by finding previous code to help me.
# not operator
print("not True = ", not True)
print("not False = ", not False)
# and operator
print("False and False = ", False and False)
print("False and True = ", False and True)
print("True and False = ", True and False)
print("True and True = ", True and True)
# or operator
print("False or False = ", False or False)
print("False or True = ", False or True)
print("True or False = ", True or False)
print("True or True = ", True or True)
print("True and not False =", True and not False)
print("False or not True = ", False or not True)
# [ ] Use relational and/or arithmetic operators with the variables x and y to write:
# 3 expressions that evaluate to True (i.e. x >= y)
# 3 expressions that evaluate to False (i.e. x <= y)
#This will use relational and arithmetic operators with the variables x and y to get 3 expressions that result in true and 3 expressions that result in false
x = 84
y = 17
# 3 expressions generating True
print(x>y)
print(y<x)
print(x!=y)
# 3 expressions generating False
print(y>x)
print(x<y)
print(x==y)
# [ ] Use the basic Boolean operators with the variables x and y to write:
# 3 expressions that evaluate to True (i.e. not y)
# 3 expressions that evaluate to False (i.e. x and y)
#This will use basic Boolean operators with the variables x and y to get 3 expressions that result in true and 3 expressions that result in false
x = True
y = False
# 3 expressions generating True
print(x==True)
print(y==False)
print(x)
# 3 expressions generating False
print(y!=False)
print(x!=True)
print(y)
# Testing if x is outside the range [10, 20]
x = 11
(x < 10) or (x > 20)
# Testing if x is outside the range [10, 20]
x = 50
(x < 10) or (x > 20)
# Testing if x is a positive and odd number
x = 11
(x > 0) and (x % 2 != 0)
# Testing if x is a positive and odd number
x = -11
(x > 0) and (x % 2 != 0)
# Testing if x is a positive and odd number
x = 22
(x > 0) and (x % 2 != 0)
# Driver information
name = 'Colette'
age = 17
# Testing if name starts with `C` and the age is 18 or less
(name.startswith('C')) and (age <= 18)
# Driver information
name = 'John'
age = 17
# Testing if name starts with `C` and the age is 18 or less
(name.startswith('C')) and (age <= 18)
# [ ] Write an expression to test if x is an even number outside the range [-100, 100]
#This will test if x is an even number outside the range [-100, 100]
# Test your expression with:
# x = 104 (True)
# x = 115 (False)
# x = -106 (True)
# x = -99 (False)
x = 104
print(((x < -100) and (x%2!=1)) or ((x>100) and (x%2!=1)))
x = 115
print(((x < -100) and (x%2!=1)) or ((x>100) and (x%2!=1)))
x = -106
print(((x < -100) and (x%2!=1)) or ((x>100) and (x%2!=1)))
x = -99
print(((x < -100) and (x%2!=1)) or ((x>100) and (x%2!=1)))
# [ ] Write an expression to test if a string s starts and ends with a capital letter
# HINT: You might find the function `str.isupper()` useful
#This will test if a string s starts and ends with a capital letter
# Test your expression with
# s = "CapitaL" (True)
# s = "Not Capital" (False)
s="CapitaL"
print((s >= 'A') and (s <= 'Z')and(s.endswith("L")))
s = "Not Capital"
print((s >= 'A') and (s <= 'Z')and(s.endswith("L")))
# [ ] Write an expression to test if a string s contains a numerical value
# then test if the value is greater than the value stored in x
# HINT: Use the functions `s.isnumeric()` and `float(s)`
#This will test if a string s contains a numerical value and then test if the value is greater than the value stored in x
# Test your expression with
# s = "39"
# x = 24
# Expression should yield True
s = "39"
x = 24
print(s.isnumeric())
s=float(s)
print(s>x)
# s = "a39"
# x = 24
# Expression should yield False
s = "a39"
x = 24
print(s.isnumeric())
x = 11
(x >= 10) and (x <= 20)
x = 30
(x >= 10) and (x <= 20)
x = 11
not((x < 10) or (x > 20))
x = 30
not((x < 10) or (x > 20))
# [ ] Write an expression equivalent to the one below to
# test if x is outside the range [10, 20] (seen in a previous example)
#This will test if x is outside the range [10, 20]
# (x < 10) or (x > 20)
# Test your expression with
# x = 11 (False)
# x = 50 (True)
x = 11
print(((x < 10) or (x > 20)))
x = 50
((x < 10) or (x > 20))
# [ ] Write a second expression to test if x is an even number outside the range [-100, 100]
# Do NOT use the expression you wrote for a previous exercise
#This will test if x is an even number outside the range [-100, 100]
# Test your expression with:
# x = 104 (True)
# x = 115 (False)
# x = -106 (True)
# x = -99 (False)
x = 104
print(((x < -100) or (x > 100)) and (x%2!=1))
x = 115
print(((x < -100) or (x > 100)) and (x%2!=1))
x = -106
print(((x < -100) or (x > 100)) and (x%2!=1))
x = -99
print(((x < -100) or (x > 100)) and (x%2!=1))
# Solicit user input
x = input("Enter an odd positive number: ")
# Convert the string input into int
x = int(x)
# Test number for validity
if ((x > 0) and (x % 2 != 0)):
print(x, "is a valid number")
else:
print(x, "is NOT a valid number")
# Solicit user input
y = input("Enter your birth year: ")
# Convert the string input into int
y = int(y)
# Check the decade membership
if (y < 1970):
print("You were born before 1970!")
elif (y >= 1970 and y < 1980):
print("You were born in the 70s!")
elif (y >= 1980 and y < 1990):
print("You were born in the 80s!")
elif (y >= 1990 and y < 2000):
print("You were born in the 90s!")
elif (y >= 2000 and y < 2010):
print("You were born in early 2000s!")
else:
print("You were born in the current decade!")
# [ ] Write a program to validate that user input is outside the range [0, 100]
#This will validate that user input is outside the range [0, 100]
x = input("Enter a number biiger than 100 or smaller than 0: ")
x = int(x)
if ((x < 0) or (x > 100)):
print(x, "is a valid number")
else:
print(x, "is not a valid number")
# [ ] Write a program to ask a user for her/his BMI index, then display the user's BMI category
#This will ask a user for her/his BMI index, then display the user's BMI category
x=input("Enter your BMI index:")
x=float(x)
if x<=18.5:
print("You are underweight")
elif (x>18.5 and x<=24.9):
print("You are a normal weight")
elif (x>=25 and x<=29.9):
print("You are overweight")
elif x>=30:
print("You are obese")