1.10 Activity 3 - Strings and I - O

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Exercise one

Write a program that outputs a times table from a given (user entered)
integer value. It should start at 2 and output only the EVEN multiples. If
the user entered the value 13, the even times table would be outputted in
the following format:

The even timetable for 13 is:


2 times 13 is 26
4 times 13 is 52
6 times 13 is 78
This program should continue until the user chooses to exit. You should
format this so that the text “times” and “is” is always in the same
column. The program should output up to and including 20 times the user
value.
My Solution:
def generate_even_times_table(number):
print(f"The even timetable for {number} is:")

for i in range(2, 21, 2):


result = i * number
print(f"{i:2d} times {number:2d} is {result:4d}")

# Main program loop


while True:
user_input = input("Enter a number (or 'exit' to quit): ")

if user_input.lower() == "exit":
break

try:
number = int(user_input)
generate_even_times_table(number)
except ValueError:
print("Invalid input. Please enter a valid number.")

print() # Add a new line for better readability

Exercise two
In the sample code given for 'Iteration' in 1.1, there is a program for
printing out a left-sided triangle of stars.

Adapt the code to create a right-sided triangle.


Adapt the code to output a diamond.
Write a menu system so the user can choose the left or right-sided
triangle, or diamond.
Enable the program to take user input for the symbol used to generate the
pattern and dictate the size of the pattern.
The output from the program should look like this:

*
* * ***
** ** *****
*** *** ***
**** **** *
Left-sided Right-sided Diamond

My Solution:
def print_left_triangle(symbol, size):
for i in range(1, size+1):
print(symbol * i)

def print_right_triangle(symbol, size):


for i in range(1, size+1):
spaces = size - i
print(" " * spaces, end="")
print(symbol * i)

def print_diamond(symbol, size):


for i in range(1, size+1):
spaces = size - i
print(" " * spaces, end="")
print(symbol * (2*i-1))

for i in range(size-1, 0, -1):


spaces = size - i
print(" " * spaces, end="")
print(symbol * (2*i-1))

# Menu
print("Pattern Generator")
print("1. Print Left Triangle")
print("2. Print Right Triangle")
print("3. Print Diamond")

choice = input("Enter your choice (1-3): ")


symbol = input("Enter the symbol: ")
size = int(input("Enter the size: "))

if choice == "1":
print_left_triangle(symbol, size)
elif choice == "2":
for i in range(1, size+1):
spaces = size - i
print(" " * spaces, end="")
print(symbol * i)
elif choice == "3":
print_diamond(symbol, size)
else:
print("Invalid choice.")

Exercise three
Write a program that outputs a calendar, given two values inputted by the
user. If the user inputs the values of 30 and 7, the following structure
will be produced, where 30 generates the number of days in the month and 7
(Sunday) indicates which day of the week the calendar starts on. The user
should only be able to input a valid range of numbers, and regardless of
the values entered the calendar should always output 7 lines (even if some
of them are blank).
My Solution:
def generate_calendar(days_in_month, starting_day):
# Validate input
if days_in_month < 28 or days_in_month > 31 or starting_day < 1 or
starting_day > 7:
print("Invalid input.")
return

# Define weekdays
weekdays = ["M", "T", "W", "Th", "F", "S", "Su"]

# Print header
for weekday in weekdays:
print(f"{weekday:3s}", end="")
print()

# Calculate number of blank spaces before the first day


first_day_position = (starting_day - 1) * 3
print(" " * first_day_position, end="")

# Calculate spacing based on the maximum number of digits in the days


of the month
max_digits = len(str(days_in_month))
spacing = " " * (max_digits + 1)

# Print calendar days


for day in range(1, days_in_month + 1):
print(f"{day:{max_digits}d}", end=" ")
# Start a new line if it's Sunday
if (starting_day + day - 1) % 7 == 0:
print()

print() # Add a new line after the calendar

# Prompt user for input


days_in_month = int(input("Enter the number of days in the month: "))
starting_day = int(input("Enter the starting day of the week (1-7): "))

# Generate calendar
generate_calendar(days_in_month, starting_day)

You might also like