Professional Documents
Culture Documents
TMA110 Kerolos Ashraf
TMA110 Kerolos Ashraf
ID:21510607
Date:30/11/2023
Q1:
a
Q1
b
pseudocode
Function calculate_factorial(input_value):
Check if the input is 0
If input_value equals 0:
Print "Error: Factorial of 0 is not defined."
Return 1
Initialize variables
Set result_factorial to 1
Set counter_value to 1
Return result_factorial
Q1
c
The input
def calculate_factorial(input_value):
if input_value == 0:
print("Error: Factorial of 0 is not defined.")
return 1
result_factorial = 1
counter_value = 1
return result_factorial
while True:
user_value = int(input("Enter a positive integer: "))
if user_value > 0:
break
else:
print("Error: Please enter a positive number.")
result_factorial = calculate_factorial(user_value)
output screenshot
Q2
input
import turtle
# Set fill and pen colors
kkerolos = 'yellow'
aaaashraffff = 'blue'
# Set the dimensions of the rectangle and the spacing between them
rectangle_width = 25
rectangle_height = 40
spacing = 10
output screenshot
Q2
step1,step2:
def write_numbers_to_file(file_name):
numbers_to_write = [15, 20, 40, 55, 135, 60]
def calculate_sum_of_numbers(file_name):
try:
with open(file_name, 'r') as file_reader:
numbers = [int(line.strip()) for line in file_reader]
total_sum = sum(numbers)
return total_sum
except FileNotFoundError:
print("File not found.")
return None
def main():
file_name_input = input("Please enter a file name: ") + 'txt'
write_numbers_to_file(file_name_input)
sum_of_numbers = calculate_sum_of_numbers(file_name_input)
if __name__ == "__main__":
main()
step3:
Part 1: write_numbers_to_file(file_name):
def write_numbers_to_file(file_name):
numbers_to_write = [15, 20, 40, 55, 135, 60]
This function is responsible for writing a list of numbers to a file. It opens the
specified file (file_name) in write mode ('w') and iterates over the
numbers_to_write list, writing each number followed by a newline character to
the file.
Part 2: calculate_sum_of_numbers(file_name):
def calculate_sum_of_numbers(file_name):
try:
with open(file_name, 'r') as file_reader:
numbers = [int(line.strip()) for line in
file_reader]
total_sum = sum(numbers)
return total_sum
except FileNotFoundError:
print("File not found.")
return None
This function reads the numbers from a file (file_name), converts them to
integers, calculates their sum, and returns the total sum. It uses a try-except
block to handle the case where the file is not found, printing an error message
and returning None.
Part 3: main():
def main():
file_name_input = input("Please enter a file name: ") +
'txt'
write_numbers_to_file(file_name_input)
sum_of_numbers = calculate_sum_of_numbers(file_name_input)
The main() function is the entry point of the program. It prompts the user to
enter a file name, appends 'txt' to it, writes numbers to the file using
write_numbers_to_file, then calculates the sum of the numbers using
calculate_sum_of_numbers. Finally, it prints the sum if it is not None.
if __name__ == "__main__":
main()
This statement checks if the script is being run directly (not imported as a
module). If true, it calls the main() function, initiating the execution of the
program.
step4:
screenshot of the code:
output screenshot:
file screenshot: