Repository navigation
Expand file tree
/
Copy pathModule03_Lecture
More file actions
133 lines (102 loc) · 3.45 KB
/
Copy pathModule03_Lecture
File metadata and controls
133 lines (102 loc) · 3.45 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'''
Module 03 Lecture Code: Decision Structures
'''
'''
# Python decision structures
# if, if-else, if-elif-else. Boolean expressions. Logical operators. Nested if statements.
# if statement
# if <condition>: The header must evaluate to True or False
# <statement> Notice the indentation
user_choice = input("Do you want to continue? (yes/no): ", )
"YeS"
#.lower() converts the string to lowercase. .upper() converts the string to uppercase
if (user_choice.upper() == "YES"): # .lower() converts the string to lowercase
print("You chose to continue.")
print("This statement belongs to parent if statement")
# belongs to paren if statement
# belongs to paren if statement
# does not belong to paren if statement
print("This statement does not belong to parent if statement")
# if-else statement
if user_choice == "no":
print("You chose to stop.")
else: # else is optional
print("You chose to continue.")
# if-elif-else statement
rand_num = int(input("Enter a number: "))
if rand_num > 5:
print("The number is greater than 5.")
elif rand_num < 5:
print("The number is less than 5.")
elif rand_num == 4:
print("The number is 4.")
else:
print("The number is 5.")
#nested if statements
if rand_num > 5:
print("The number is between 5 and 10.")
if rand_num > 7:
print("The number is greater than 7.")
if rand_num == 4:
print("The number is 8.")
else:
print("The number is less than 5.")
# logical operators
# and, or, not
# >, <, >=, <=, ==, !=
# truth tables
# T and T = T I am 5.9 feet tall and I am 45 years old
# T and F = F I am 5.9 feet tall and I am 4 years old
# F and T = F I am 4 feet tall and I am 45 years old
# F and F = F I am 4 feet tall and I am 4 years old
# T or T = T I am 5.9 feet tall or I am 45 years old
# T or F = T I am 5.9 feet tall or I am 4 years old
# F or T = T I am 4 feet tall or I am 45 years old
# F or F = F I am 4 feet tall or I am 4 years old
# 1 && 1 = 1
# 1 && 0 = 0
# 0 && 1 = 0
# 0 && 0 = 0
#short-circuit
choice = input("Do you want to play a game? ")
my_num = int(input("Enter a number between 0 - 10: "))
if (choice == "yes" and my_num >= 4): # F
print("You want to play the game")
new_num = int(input("Enter a number from 0 - 250: "))
#
# counter / accumulator
counter = 0
# Now let's use the logical operators in our if statements
age = int(input("Enter your age: "))
height = float(input("Enter your height in feet: "))
choice = "bad"
if age >= 12 and age <= 65 and new_num == 21:
print("Happy 21st birthday")
counter += 1 # counter = counter + 1
else:
print("You are not tall enough or old enough to ride the roller coaster.")
# You try it using the OR operator
'''
counter = 0
riddle_1 = "Heaven"
riddle_2 = "A map"
riddle_3 = "A candle"
answer_1 = input("What is the answer to the riddle: Song by Brian Adams")
answer_2 = input("What is the answer to a anolog directional paper device")
answer_3 = input("What is the answer to a anolog light source pre-dating electricity")
GRAVITY = 9.8
print(GRAVITY)
if answer_1 == riddle_1:
print("You are correct!")
counter += 1
if answer_2 == "A map":
print("You are correct!")
counter += 1
if answer_3 == "A candle":
print("You are correct!")
counter += 1
print("You got", counter, "correct answers.")
print(f"You got {counter} correct answers.")
GRAVITY = 2
print(GRAVITY)
# %n.nf %d %s %f