Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Name of Student:Kerolos Ashraf Mores

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

Calculate factorial using a while loop


While counter_value is less than or equal to input_value:
Multiply result_factorial by counter_value
Increment counter_value by 1

Return result_factorial

Read input from the user, ensuring it's positive


While True:
Display "Enter a positive integer: "
Read user_value from input as an integer
If user_value is greater than 0:
Break out of the loop
Else:
Print "Error: Please enter a positive number."

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

while counter_value <= input_value:


result_factorial *= counter_value
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)

print(f"The factorial of {user_value} is: {result_factorial}")

output screenshot

Q2
input
import turtle
# Set fill and pen colors
kkerolos = 'yellow'
aaaashraffff = 'blue'

# Set up the turtle


cr7 = turtle.Turtle()
cr7.penup()
cr7.goto(-150, 0)
cr7.pendown()

# Set the pen size


cr7.pensize(3)

# Set the initial heading of the turtle


cr7.setheading(0)

# Set the dimensions of the rectangle and the spacing between them
rectangle_width = 25
rectangle_height = 40
spacing = 10

# Hide the turtle


cr7.hideturtle()

# Draw the shapes


for i in range(8):
# Set colors based on whether the iteration is even or odd
if i % 2 == 0:
cr7.fillcolor(kkerolos)
cr7.pencolor(aaaashraffff)

# Start drawing the rectangle


cr7.begin_fill()
cr7.forward(rectangle_width)
cr7.left(90)
cr7.forward(rectangle_height)
cr7.left(90)
cr7.forward(rectangle_width)
cr7.left(90)
cr7.forward(rectangle_height)
cr7.left(90)
cr7.end_fill()

# Move the turtle to the next position


cr7.penup()
cr7.forward(rectangle_width + spacing)
cr7.pendown()

# Rotate the turtle to a specific angle


cr7.right(45)

# Finish the program


turtle.done()

output screenshot

Q2
step1,step2:

def write_numbers_to_file(file_name):
numbers_to_write = [15, 20, 40, 55, 135, 60]

with open(file_name, 'w') as file_writer:


for num in numbers_to_write:
file_writer.write(f"{num}\n")

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 sum_of_numbers is not None:


print(f'The sum of numbers in the file is:
{sum_of_numbers}')

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]

with open(file_name, 'w') as file_writer:


for num in numbers_to_write:
file_writer.write(f"{num}\n")

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)

if sum_of_numbers is not None:


print(f'The sum of numbers in the file is:
{sum_of_numbers}')

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.

Part 4: if __name__ == "__main__":

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:

You might also like