Project 3 Quiz

py

School

California State University, Long Beach *

*We aren’t endorsed by this school

Course

174

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

2

Uploaded by ajkseo

Report
# Project 3 Quiz 1. Write a function that creates a rectangle that takes in the following arguments - height - width - symbol: the character that will be used in the rectangle Where the symbols are alternating in the following way: Example on a 4 by 4 rectangle, calling `dotted_rectangle(4, 4, symbol)` ``` * * * * * * * * ``` ```python def dotted_rectangle(height, width, symbol): for i in range(height): for j in range(width): if (i + j) % 2 == 0: print(symbol, end=" ") else: print(" ", end=" ") print() ``` 2. Refactoring: You have code that looks like this ```python # code above MENU_1 = 'Menu Options 1...' MENU_2 = 'Menu Options 2 ...' if option1 == 1: option2 = input() while option2 not in '#@!*': print('Invalid Entry - try again') print(MENU_1) option2 = input() elif option1 == 2: option2 = input() while option2 not in '123': print('Invalid Entry - try again') print(MENU_2) option2 = input() ``` Our goal is to not repeat ourselves. Write a function named valid_input that takes in the arguments - valid: a string of valid characters - menu: a string representing the menu message and returns the valid input. This function should be able to replace the duplicate code above ```python def valid_input(valid, menu_str): while True: option2 = input() if option2 in valid: return option2 else:
print("Invalid Entry - Try Again") print(menu_str) MENU_1 = 'Menu Options 1...' MENU_2 = 'Menu Options 2 ...' if option1 == 1: option = valid_input('#@!*', MENU_1) print(valid) elif option1 == 2: option2 = valid_input('123', MENU_2) print(menu_str) ``` 3. Write a function that creates a half pyramid. It takes in the following input: - height: the height of the - symbol: the character that will be used in the pyramid AND the diagonal line counts up from one like this when calling `counting_half_pyramid(4, '*')`: ``` 1 * 2 * * 3 * * * 4 ``` ```python def counting_half_pyramid(height, symbol): for i in range(1,n+1): print(height) return user_input ```
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help