0% found this document useful (0 votes)
38 views4 pages

1.10 Activity 3 - Strings and I - O

The document contains 3 programming exercises: 1. Write a program to output an even times table for a user-entered integer up to 20, formatting the output. 2. Adapt a program to print left/right triangles and diamonds of a user-entered symbol, and allow the user to choose the shape. 3. Write a program to output a calendar for a month with a user-entered number of days and starting day of the week, formatting the output in a 7-line structure.

Uploaded by

Bui Linh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

1.10 Activity 3 - Strings and I - O

The document contains 3 programming exercises: 1. Write a program to output an even times table for a user-entered integer up to 20, formatting the output. 2. Adapt a program to print left/right triangles and diamonds of a user-entered symbol, and allow the user to choose the shape. 3. Write a program to output a calendar for a month with a user-entered number of days and starting day of the week, formatting the output in a 7-line structure.

Uploaded by

Bui Linh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy