SE 100 Assignment 6
SE 100 Assignment 6
Student Section: M5
Question 1: Write a statement that creates a list with the following strings: 'Einstein',
'Newton', 'Copernicus', and 'Kepler'.
Question 2: Assume names references a list. Write a for loop that displays each element of the list.
Question 3: Assume the list numbers1 has 100 elements, and numbers2 is an empty list. Write
code that copies the values in numbers1 to numbers2.
numbers2 = []
numbers2.append(num)
SE 100 – Assignment 6 – Fall 2023-2024
Question 4: Draw a flowchart showing the general logic for totaling the values in a list.
Start
Start the
accumulator
Check if
there is Yes
another Read the next Add the number to
number in number the accumulator
the list
No
End
Question 5: Write a function that accepts a list as an argument (assume the list contains integers)
and returns the total of the values in the list.
def calculate_total(input_list):
total = 0
Page 2 of 14
SE 100 – Assignment 6 – Fall 2023-2024
total += num
return total
my_list = [1, 2, 3, 4, 5]
result = calculate_total(my_list)
Question 6: Assume the names variable references a list of strings. Write code that determines
whether 'Ruby' is in the names list. If it is, display the message 'Hello Ruby'. Otherwise,
display the message 'No Ruby'.
if 'Ruby' in names:
print('Hello Ruby')
else:
print('No Ruby')
Page 3 of 14
SE 100 – Assignment 6 – Fall 2023-2024
[40,50,60,10,20,30]
Question 8: Assume list1 is a list of integers. Write a statement that uses a list comprehension to
create a second list containing the squares of the elements of list1.
list1 = [1, 2, 3, 4, 5]
squared_list = [x ** 2 for x in list1]
Question 9: Assume list1 is a list of integers. Write a statement that uses a list comprehension to
create a second list containing the elements of list1 that are greater than 100.
Question 10: Assume list1 is a list of integers. Write a statement that uses a list comprehension
to create a second list containing the elements of list1 that are even numbers.
Question 11: Write a statement that creates a two-dimensional list with 5 rows and 3 columns. Then
write nested loops that get an integer value from the user for each element in the list.
while True:
value = input(f"Enter the value for element at row {row+1}, column {col+1}: ")
Page 4 of 14
SE 100 – Assignment 6 – Fall 2023-2024
if value.isdigit():
matrix[row][col] = int(value)
break
else:
print(row)
Page 5 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Question 12 - Lottery Number Generator: Design a program that generates a seven-digit lottery
number. The program should generate seven random numbers, each in the range of 0 through 9,
and assign each number to a list element. Then write another loop that displays the numbers
separated by commas in a single line.
Write the code here and attach a screenshot of the output.
import random
lottery_numbers = []
for _ in range(7):
random_number = random.randint(0, 9)
lottery_numbers.append(random_number)
Page 6 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Question 13 - Rainfall Statistics: Design a program that lets the user enter the total rainfall for each
of 12 months into a list. The program should calculate and display the total rainfall for the year, the
average monthly rainfall, and the months with the highest and lowest amounts.
Write the code here and attach a screenshot of the output using some sample data as an example.
monthly_rainfall = []
valid_input = False
rainfall = float(rainfall_input)
monthly_rainfall.append(rainfall)
valid_input = True
else:
total_rainfall = sum(monthly_rainfall)
average_rainfall = total_rainfall / 12
max_rainfall_month = monthly_rainfall.index(max(monthly_rainfall)) + 1
min_rainfall_month = monthly_rainfall.index(min(monthly_rainfall)) + 1
Page 7 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Question 14 - Number Analysis Program: Design a program that asks the user to enter a series of 20
numbers. The program should store the numbers in a list then display the following data:
Write the code here and attach a screenshot of the output using some sample data as an example.
numbers = []
for i in range(20):
valid_input = False
num = float(num_input)
numbers.append(num)
valid_input = True
else:
lowest = min(numbers)
highest = max(numbers)
total = sum(numbers)
average = total / 20
Page 8 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Page 9 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Question 15 - Larger Than n: In a program, write a function that accepts two arguments: a list, and a
number n. Assume that the list contains numbers. The function should display all of the numbers in
the list that are greater than the number n.
if num > n:
print(num)
n = 15
display_numbers_greater_than_n(my_list, n)
Page 10 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Question 16 - Lo Shu Magic Square: The Lo Shu Magic Square is a grid with 3 rows and 3 columns.
The Lo Shu Magic Square has the following properties:
The sum of each row, each column, and each diagonal all add up to the same number.
Example:
In a program you can simulate a magic square using a two-dimensional list. Write a function that
accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic
Square. Test the function in a program.
def is_loshu_magic_square(grid):
if len(grid) != 3 or len(grid[0]) != 3:
return False
row_sum = sum(grid[0])
for i in range(3):
if sum(grid[i]) != row_sum:
return False
for j in range(3):
col_sum = grid[0][j] + grid[1][j] + grid[2][j]
if col_sum != row_sum:
return False
Page 11 of 14
SE 100 – Assignment 6 – Fall 2023-2024
if is_loshu_magic_square(magic_square):
print("The grid is a Lo Shu Magic Square.")
else:
print("The grid is not a Lo Shu Magic Square.")
if is_loshu_magic_square(not_magic_square):
print("The grid is a Lo Shu Magic Square.")
else:
print("The grid is not a Lo Shu Magic Square.")
Page 12 of 14
SE 100 – Assignment 6 – Fall 2023-2024
Question 17 - Prime Number Generation: A positive integer greater than 1 is said to be prime if it
has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime.
Write a program that asks the user to enter an integer greater than 1, then displays all of the prime
numbers that are less than or equal to the number entered. The program should work as follows:
Once the user has entered a number, the program should populate a list with all of the
integers from 2 up through the value entered.
The program should then use a loop to step through the list. The loop should pass each
element to a function that displays whether the element is a prime number, or a composite
number.
Write the code here and attach a screenshot of the output using some sample data as an example.
def is_prime(num):
if num <= 1:
return False
if num <= 3:
return True
if num % 2 == 0 or num % 3 == 0:
return False
i=5
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True
def display_prime_numbers(limit):
prime_numbers = []
if is_prime(num):
prime_numbers.append(num)
return prime_numbers
if n.isdigit():
n = int(n)
if n <= 1:
else:
prime_numbers = display_prime_numbers(n)
if prime_numbers:
else:
else:
Page 14 of 14