Skip to content

Commit 4ab8d72

Browse files
committed
Add conditional exercises to demonstrate control flow in Python
1 parent 5bbef29 commit 4ab8d72

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

10_Conditionals/10_conditional.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Question No 1.
2+
# Get a score value from the user.
3+
userScore = input("Give me a score value: \n")
4+
print("User score (as string):", userScore) # Correct variable name: userScore
5+
6+
# Convert to integer.
7+
userScore_in_int = int(userScore)
8+
print("User score (as integer):", userScore_in_int)
9+
10+
# Ask user's age and determine category.
11+
age = input("Please provide me age: ")
12+
age = int(age)
13+
if age < 13:
14+
print("chai")
15+
elif age <= 20:
16+
print("Teenager")
17+
elif age < 60:
18+
print("Adult")
19+
else:
20+
print("senior")
21+
22+
23+
# Question No 2.
24+
age = 22
25+
day = "wednesday"
26+
price = 12 if age >= 18 else 8
27+
28+
# Correct indentation and use correct comparison operator.
29+
if day == "wednesday":
30+
price = price - 2
31+
# Alternatively: price -= 2
32+
33+
print("Ticket price for you is $:", price)
34+
35+
36+
# Question No 3.
37+
score = 85
38+
if score >= 101:
39+
print("You are not allowed to input grade greater than 100")
40+
exit()
41+
42+
if score >= 90:
43+
grade = "A"
44+
elif score >= 80:
45+
grade = "B"
46+
elif score >= 70:
47+
grade = "C"
48+
elif score >= 60:
49+
grade = "D"
50+
else:
51+
grade = "F"
52+
53+
print("Grade:", grade)
54+
55+
56+
# Question No 4.
57+
fruit = "Banana"
58+
color = "Yellow"
59+
if fruit == "Banana":
60+
if color == "Green":
61+
print("Unripe")
62+
elif color == "Yellow":
63+
print("Ripe")
64+
elif color == "Brown":
65+
print("Overripe")
66+
else:
67+
print("Nothing")
68+
69+
70+
# Question No 5.
71+
weather = "Sunny"
72+
# Use '==' for comparisons, not '='.
73+
if weather == "Sunny":
74+
activity = "Go for a walk"
75+
elif weather == "Rainy":
76+
activity = "Read a book"
77+
elif weather == "Snowy":
78+
activity = "Build a snowman"
79+
else:
80+
activity = "Stay indoors"
81+
82+
print("Activity:", activity)
83+
84+
85+
# Question No 6.
86+
distance = 5
87+
if distance < 3:
88+
transport = "walk"
89+
elif distance <= 15:
90+
transport = "Bike"
91+
else:
92+
transport = "Car"
93+
94+
print("AI recommends you the transport:", transport)
95+
96+
97+
# Question No 7.
98+
order_size = "Medium"
99+
extra_shot = True
100+
101+
# Use consistent variable name for coffee.
102+
if extra_shot:
103+
coffee = order_size + " coffee with an extra shot"
104+
else:
105+
coffee = order_size + " coffee"
106+
107+
print("Order:", coffee)
108+
109+
110+
# Question No 8.
111+
password = "Secure3P@ss"
112+
113+
if len(password) < 6:
114+
strength = "weak"
115+
elif len(password) <= 10:
116+
strength = "Medium"
117+
else:
118+
strength = "Strong"
119+
120+
print("Password strength is:", strength)
121+
122+
123+
# Question No 9.
124+
year = 2023
125+
126+
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
127+
print(year, "is a leap year")
128+
else:
129+
print(year, "is not a leap year")

10_Conditionals/questions.md

Whitespace-only changes.

0 commit comments

Comments
 (0)