8000 finish coding section B for session 03 · Pcodias/intro-to-python@335aa94 · GitHub
[go: up one dir, main page]

Skip to content

Commit 335aa94

Browse files
author
Replit user
committed
finish coding section B for session 03
1 parent 8c51f53 commit 335aa94

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

session_03/exercises_3.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@
9292
## Section B
9393
# 1. Ask for the user's name, if they are called "Alice" print "Hello, Alice", if they are called "Bob",
9494
# print "You're not Bob! I'm Bob", else print "You must be Charlie".
95+
name = input("What is your name? ")
96+
97+
if name == "Alice":
98+
print("Hello, Alice")
99+
elif name == "Bob":
100+
print("You're not Bob! I'm Bob")
101+
else:
102+
print("You must be Charlie")
95103

96104

97105

@@ -100,31 +108,140 @@
100108
# 2. If they are between 11 and 16, print "You can can come to this school"
101109
# 3. If they are over 16, print 'You're too old for school"
102110
# 4. If they are 0, print "You're not born yet!"
111+
age = int(input("Enter your age: "))
112+
113+
if age < 0:
114+
print("Invalid age entered.")
115+
elif age < 11:
116+
print("You're too young to go to this school.")
117+
elif age <= 16:
118+
print("You can come to this school.")
119+
elif age > 16:
120+
print("You're too old for school.")
121+
elif age == 0:
122+
print("You're not born yet!")
103123

104124

105125

106126
# 3. Ask the user to enter the name of a month. If the user enters March/April/May: print "<month> is in Spring", otherwise print "I don't know".
107127
# 1. Expand for the rest of the year, given that summer is June/July/August. Autumn is September/October/November. Winter is December/January/February.
108128
# 2. Ensure that when an unknown month is given it prints "I don't know".
129+
month = input("Enter the name of a month: ")
130+
131+
if month.lower() in ["march", "april", "may"]:
132+
print(month + " is in Spring.")
133+
elif month.lower() in ["june", "july", "august"]:
134+
print(month + " is in Summer.")
135+
elif month.lower() in ["september", "october", "november"]:
136+
print(month + " is in Autumn.")
137+
elif month.lower() in ["december", "january", "february"]:
138+
print(month + " is in Winter.")
139+
else:
140+
print("I don't know.")
109141

110142

111143

112144
# 4. Ask the user for two different numbers, if both numbers are even, print "Even", if both numbers are odd, print "Odd", else print the product of the two numbers.
145+
num1 = int(input("Enter the first number: "))
146+
num2 = int(input("Enter the second number: "))
147+
148+
if num1 % 2 == 0 and num2 % 2 == 0:
149+
print("Even")
150+
elif num1 % 2 != 0 and num2 % 2 != 0:
151+
print("Odd")
152+
else:
153+
print(num1 * num2)
113154

114155

115156

116157
# 5. Ask the user to input two numbers. Decide which is the number of highest value and print this out.
158+
num1 = float(input("Enter the first number: "))
159+
num2 = float(input("Enter the second number: "))
160+
161+
if num1 > num2:
162+
print("The first number,", num1, "is the highest.")
163+
elif num2 > num1:
164+
print("The second number,", num2, "is the highest.")
165+
else:
166+
print("Both numbers are equal.")
117167

118168

119169

120170
# 6. You have had a fantastic year and are now going to offer a bonus of 20% to any employee who has a service of over 7 years,
121171
# a bonus of 15% to any employee who has a service of over 5 years and a bonus of 10% to any employee who has a service of 3 - 5 years.
122172
# Ask the user to input their current salary and years of service and print out their salary and their bonus or "No bonus" if they are not receiving one.
173+
current_salary = float(input("Enter your current salary: "))
174+
years_of_service = int(input("Enter your years of service: "))
175+
176+
bonus = 0
177+
178+
if years_of_service > 7:
179+
bonus = 0.2
180+
elif years_of_service > 5:
181+
bonus = 0.15
182+
elif 3 <= years_of_service <= 5:
183+
bonus = 0.1
184+
185+
if bonus > 0:
186+
bonus_amount = current_salary * bonus
187+
new_salary = current_salary + bonus_amount
188+
print("Your salary: $", current_salary)
189+
print("Bonus: $", bonus_amount)
190+
print("New salary with bonus: $", new_salary)
191+
else:
192+
print("No bonus")
123193

124194

125195

126196
# 7. Take the age and name of three people and determine who is the oldest and youngest and print out the name and age of the oldest and youngest.
127197
# If all three ages are the same, print that.
198+
# Get the details of the first person
199+
name1 = input("Enter the name of the first person: ")
200+
age1 = int(input("Enter the age of the first person: "))
201+
202+
# Get the details of the second person
203+
name2 = input("Enter the name of the second person: ")
204+
age2 = int(input("Enter the age of the second person: "))
205+
206+
# Get the details of the third person
207+
name3 = input("Enter the name of the third person: ")
208+
age3 = int(input("Enter the age of the third person: "))
209+
210+
# Determine the oldest person
211+
oldest_age = max(age1, age2, age3)
212+
oldest_names = []
213+
214+
# Determine the youngest person
215+
youngest_age = min(age1, age2, age3)
216+
youngest_names = []
217+
218+
# Find the names of the oldest and youngest people
219+
if age1 == oldest_age:
220+
oldest_names.append(name1)
221+
if age2 == oldest_age:
222+
oldest_names.append(name2)
223+
if age3 == oldest_age:
224+
oldest_names.append(name3)
225+
226+
if age1 == youngest_age:
227+
youngest_names.append(name1)
228+
if age2 == youngest_age:
229+
youngest_names.append(name2)
230+
if age3 == youngest_age:
231+
youngest_names.append(name3)
232+
233+
# Print the results
234+
print("Oldest person(s):")
235+
for name in oldest_names:
236+
print(f"Name: {name}, Age: {oldest_age}")
237+
238+
print("Youngest person(s):")
239+
for name in youngest_names:
240+
print(f"Name: {name}, Age: {youngest_age}")
241+
242+
# If all three ages are the same
243+
if age1 == age2 == age3:
244+
print("All three people have the same age.")
128245

129246

130247

@@ -136,3 +253,40 @@
136253
# e. 25 to 45 – E
137254
# f. Below 25 - F
138255
# Ask user to enter the lesson and the marks for three lessons and print out the corresponding grades for the lesson.
256+
# Get the details for the first lesson
257+
lesson1 = input("Enter the name of the first lesson: ")
258+
marks1 = float(input("Enter the marks for the first lesson: "))
259+
260+
# Get the details for the second lesson
261+
lesson2 = input("Enter the name of the second lesson: ")
262+
marks2 = float(input("Enter the marks for the second lesson: "))
263+
264+
# Get the details for the third lesson
265+
lesson3 = input("Enter the name of the third lesson: ")
266+
marks3 = float(input("Enter the marks for the third lesson: "))
267+
268+
# Function to calculate the grade based on marks
269+
def calculate_grade(marks):
270+
if marks > 80:
271+
return "A"
272+
elif 60 <= marks <= 80:
273+
return "B"
274+
elif 50 <= marks < 60:
275+
return "C"
276+
elif 45 <= marks < 50:
277+
return "D"
278+
elif 25 <= marks < 45:
279+
return "E"
280+
else:
281+
return "F"
282+
283+
# Calculate grades for each lesson
284+
grade1 = calculate_grade(marks1)
285+
grade2 = calculate_grade(marks2)
286+
grade3 = calculate_grade(marks3)
287+
288+
# Print the results
289+
print("Grades for each lesson:")
290+
print(f"{lesson1}: {grade1}")
291+
print(f"{lesson2}: {grade2}")
292+
print(f"{lesson3}: {grade3}")

0 commit comments

Comments
 (0)
0