-
Notifications
You must be signed in to change notification settings - Fork 0
/
Control Flow in Python - If Elif Else Statements.
92 lines (72 loc) · 2.26 KB
/
Control Flow in Python - If Elif Else Statements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Conditional Statements:
# Basic:
temperature = 31.2
if temperature > 30:
print("It's hot!")
print("Drink plenty of water!")
elif temperature > 20:
print("It's nice!")
else:
print("It's cold!")
print("Dress accordingly!")
print("Done!")
# I want to spice things a little:
print('Enter temperature in integers or floats or enter "quit" to exit:')
while True:
temperature = input("> ")
if temperature.lower() == "quit":
break
try:
temperature = float(temperature)
if temperature >= 58:
print("Call the fire department if it really is so hot!")
break
elif temperature > 40:
print("It's too hot outside, stay inside if possible!")
print("Have plenty of water with you and stay under shade!")
elif temperature > 25:
print("It's hot outside! Stay hydrated and under shade!")
elif temperature >= 18:
print("Can't get better than that! Call a friend!")
elif temperature > 4:
print("It's cold ouside! Dress accordinly!")
else:
print("It's too cold outside! Dress yourself warm and tide!")
except ValueError:
print("Enter integer or floats only, please!")
print("Done!")
# Ternary Operator:
age = int(input("Enter your age: "))
message = "Eligible!" if age >= 18 else "Not eligible!"
print(message)
# In a while loop with input^:
print('Enter your age or "quit" for exit: ')
while True:
age = input("> ")
if age.lower() == "quit":
break
try:
age = int(age)
message = "Eligible!" if age >= 18 else "Not eligible!"
print(message)
except ValueError:
print("Integers only, please!")
print("Done!")
# Logical Operators:
high_income = False
good_credit = False
student = True
if high_income and good_credit and not student:
print("Eligible for a higher loan!")
elif high_income and good_credit and student:
print("Eligible for a student loan!")
elif (high_income or good_credit) and not student:
print("Eligible for a lower loan!")
else:
print("Not eligble for a loan!")
# Short-circuit Evaluation:
# 1. Logical Operators (and, or, not) are short-circuit.
# Chaining Comparison Operators:
age = 22
if 18 <= age < 65:
print("Eligible!")