Input 0: 1, Type: <class 'int'>
Input 1: ae, Type: <class 'str'>
Input 2: 2, Type: <class 'int'>
Input 3: be, Type: <class 'str'>
Input 4: 3, Type: <class 'int'>
import math
imported_math = False
# check if math module is already imported
for i, cell in enumerate(In):
if 'import math' in cell:
imported_math = True
break
# import math module if not already imported
if not imported_math:
import math
# create a list with integers
my_list = [str(1), 2, 3, 4, 5, [6, 7]]
# loop through the list and try to access the length attribute of each element
for element in my_list:
try:
length = len(element)
print(f"The length of {element} is {length}")
except TypeError:
try:
length = math.sqrt(element)
print(f"The square root of {element} is {length}")
except TypeError:
print(f"{element} does not have a length attribute or a square root. It is not a string, a sequence, or a number.")
The length of 1 is 1
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0
The square root of 5 is 2.23606797749979
The length of [6, 7] is 2
# create a dictionary with sample data
data = {'Alice': [25, 'A'], 'Bob': [30, 'B'], 'Charlie': [35, 'C']}
# iterate over the dictionary
for name, info in data.items():
print(f"Name: {name}")
for i, value in enumerate(info):
if i == 0 and value > 30:
print(f"Skipping {name} because age is greater than 30")
break
elif i == 1 and value == 'B':
print(f"Skipping {name} because grade is B")
continue
print(f"Info {i}: {value}")
Name: Alice
Info 0: 25
Info 1: A
Name: Bob
Info 0: 30
Skipping Bob because grade is B
Name: Charlie
Skipping Charlie because age is greater than 30
{'Alice': 'A', 'Bob': 'B', 'Charlie': 'C', 'David': 'D', 'Eve': 'A'}
import random
passing_grade = 70
students = {}
student_names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry', 'Isabella', 'Jack']
num_students = int(input("How many students do you want to add? "))
for i in range(num_students):
if not student_names:
print("Error: Not enough student names.")
break
name = student_names.pop(0)
grade = random.randint(50, 100)
students[name] = grade
print(students)
highest_grade = 0
passed_students = []
for name, grade in students.items():
if grade > highest_grade:
highest_grade = grade
if grade >= passing_grade:
passed_students.append(name)
print(f"The highest grade is {highest_grade}")
print(f"The students who passed are: {passed_students}")
Error: Not enough student names.
{'Alice': 57, 'Bob': 79, 'Charlie': 94, 'David': 89, 'Eve': 97, 'Frank': 70, 'Grace': 50, 'Henry': 75, 'Isabella': 67, 'Jack': 93}
The highest grade is 97
The students who passed are: ['Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Henry', 'Jack']
# Section 3
import random
passing_grade = 70
students = {}
student_names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry', 'Isabella', 'Jack', 'Kate', 'Liam']
num_students = min(int(input("How many students do you want to add? ")), 12)
for i in range(num_students):
if not student_names:
print("Error: Not enough student names.")
break
name = student_names.pop(0)
grade = round(random.uniform(50, 100), 1)
students[name] = grade
print(students)
highest_grade = 0
passed_students = []
for name, grade in students.items():
if grade > highest_grade:
highest_grade = grade
if grade >= passing_grade:
passed_students.append(name)
print(f"The highest grade is {highest_grade}")
print(f"The students who passed are: {passed_students}")
{'Alice': 64.5, 'Bob': 75.6, 'Charlie': 60.1, 'David': 91.0, 'Eve': 86.0, 'Frank': 53.8, 'Grace': 95.4, 'Henry': 59.5, 'Isabella': 51.6, 'Jack': 97.1, 'Kate': 74.0, 'Liam': 58.6}
The highest grade is 97.1
The students who passed are: ['Bob', 'David', 'Eve', 'Grace', 'Jack', 'Kate']
# Section 4 Bonus
import random
passing_grade = 70
students = {}
student_names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry', 'Isabella', 'Jack', 'Kate', 'Liam']
num_students = min(int(input("How many students do you want to add? ")), 12)
for i in range(num_students):
if not student_names:
print("Error: Not enough student names.")
break
name = student_names.pop(0)
while True:
try:
grade = int(input(f"Enter grade for {name}: "))
break
except ValueError:
print("Invalid input. Please enter an integer.")
students[name] = grade
print(students)
highest_grade = 0
passed_students = []
for name, grade in students.items():
if grade > highest_grade:
highest_grade = grade
if grade >= passing_grade:
passed_students.append(name)
print(f"The highest grade is {highest_grade}")
print(f"The students who passed are: {passed_students}")
Invalid input. Please enter an integer.
{'Alice': 14, 'Bob': 32, 'Charlie': 97, 'David': 86, 'Eve': 97, 'Frank': 96, 'Grace': 98, 'Henry': 76, 'Isabella': 87, 'Jack': 76, 'Kate': 74, 'Liam': 70}
The highest grade is 98
The students who passed are: ['Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry', 'Isabella', 'Jack', 'Kate', 'Liam']