|
92 | 92 | ## Section B
|
93 | 93 | # 1. Ask for the user's name, if they are called "Alice" print "Hello, Alice", if they are called "Bob",
|
94 | 94 | # 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") |
95 | 103 |
|
96 | 104 |
|
97 | 105 |
|
|
100 | 108 | # 2. If they are between 11 and 16, print "You can can come to this school"
|
101 | 109 | # 3. If they are over 16, print 'You're too old for school"
|
102 | 110 | # 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!") |
103 | 123 |
|
104 | 124 |
|
105 | 125 |
|
106 | 126 | # 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".
|
107 | 127 | # 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.
|
108 | 128 | # 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.") |
109 | 141 |
|
110 | 142 |
|
111 | 143 |
|
112 | 144 | # 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) |
113 | 154 |
|
114 | 155 |
|
115 | 156 |
|
116 | 157 | # 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.") |
117 | 167 |
|
118 | 168 |
|
119 | 169 |
|
120 | 170 | # 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,
|
121 | 171 | # 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.
|
122 | 172 | # 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") |
123 | 193 |
|
124 | 194 |
|
125 | 195 |
|
126 | 196 | # 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.
|
127 | 197 | # 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.") |
128 | 245 |
|
129 | 246 |
|
130 | 247 |
|
|
136 | 253 | # e. 25 to 45 – E
|
137 | 254 | # f. Below 25 - F
|
138 | 255 | # 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