-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLecture_1_Conditionals.py
160 lines (133 loc) · 2.91 KB
/
Lecture_1_Conditionals.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#==================Calculator.py===========================
#basic conditional code using if, elif and else
x = int(input("What's x?"))
y = int(input("What's y?"))
if x < y:
print ("x is less than y")
elif x > y:
print ("x is greater than y")
else:
print ("x is equal to y")
#basic conditional code using if, or and else
x = int(input("What's x?"))
y = int(input("What's y?"))
if x < y or x > y:
print("x is not equal to y")
else:
print ("x is equal to y")
#making it more easy and compact
x = int(input("What's x?"))
y = int(input("What's y?"))
if x!=y:
print("x is not equal to y")
else:
print ("x is equal to y")
#==================Grade.py===========================
#using and as conditional
score = int(input("Score: "))
if score >= 90 and score <= 100:
print("Grade: A")
elif score >= 80 and score <= 90:
print("Grade: B")
elif score >= 70 and score <= 80:
print("Grade: C")
elif score >= 60 and score <= 80:
print("Grade: D")
else:
print("Grade: F")
#simplifying above code
score = int(input("Score: "))
if 90 <= score <= 100:
print("Grade: A")
elif 80 <= score <= 90:
print("Grade: B")
elif 70 <= score <= 80:
print("Grade: C")
elif 60 <= score <= 70:
print("Grade: D")
else:
print("Grade: F")
=================================
#1
x = int(input("What's x?"))
if x % 2 == 0:
print("Even")
else:
print ("Odd")
#2
def main():
x = int(input("What's x?"))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
if n % 2 == 0:
return True
else:
return False
main()
#3
def main():
x = int(input("What's x?"))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
return True if n % 2 == 0 else False
main()
#4
def main():
x = int(input("What's x?"))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
return n % 2 == 0
main()
===========================================
#1
name = input("What's your name?")
if name == "Harry":
print ("Gryffindor")
elif name == "Hermione":
print ("Gryffindor")
elif name == "Ron":
print ("Gryffindor")
elif name == "Draco":
print ("Slytherin")
else:
print ("Who?")
#2
name = input("What's your name?")
if name == "Harry" or name == "Hermione" or name == "Ron":
print ("Gryffindor")
elif name == "Draco":
print ("Slytherin")
else:
print ("Who?")
#3
name = input("What's your name?")
match name:
case "Harry":
print("Gryffindor")
case "Hermione":
print("Gryffindor")
case "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")
#4
name = input("What's your name?")
match name:
case "Harry" | "Hermione" | "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")
======================================