Write a function that prints a square with the character #.Prototype: def print_square(size):size is the size length of the squaresize must be an integer, otherwise raise a TypeError exception with the message size must be an integerif size is less than 0, raise a ValueError exception with the message size must be >= 0if size is a float and is less than 0, raise a TypeError exception with the message size must be an integerYou are not allowed to import any module
Question
Write a function that prints a square with the character #.Prototype: def print_square(size):size is the size length of the squaresize must be an integer, otherwise raise a TypeError exception with the message size must be an integerif size is less than 0, raise a ValueError exception with the message size must be >= 0if size is a float and is less than 0, raise a TypeError exception with the message size must be an integerYou are not allowed to import any module
Solution
Sure! Here is a step-by-step solution to the problem:
- Define the function
print_squarewith a single parametersize. - Check if
sizeis an integer using theisinstance()function. If it is not, raise aTypeErrorexception with the message "size must be an integer". - Check if
sizeis less than 0. If it is, raise aValueErrorexception with the message "size must be >= 0". - Check if
sizeis a float using theisinstance()function. If it is, check if it is less than 0. If both conditions are true, raise aTypeErrorexception with the message "size must be an integer". - If all the checks pass, proceed to print the square.
- Use a loop to iterate
sizetimes to print each row of the square. - Within the loop, use another loop to print
sizenumber of "#" characters to form each row of the square. - After printing each row, use the
print()function with no arguments to move to the next line. - Test the function with different inputs to ensure it works correctly.
Here is the code implementation:
def print_square(size):
if not isinstance(size, int):
raise TypeError("size must be an integer")
if size < 0:
raise ValueError("size must be >= 0")
if isinstance(size, float) and size < 0:
raise TypeError("size must be an integer")
for _ in range(size):
print("#" * size)
# Testing the function
print_square(5) # Prints a square of size 5x5
print_square(3) # Prints a square of size 3x3
print_square(0) # Prints an empty square
This function will print a square of "#" characters with the given size.
Similar Questions
Write a function that prints a square with the character #.Prototype: def print_square(size):size is the size length of the squaresize must be an integer, otherwise raise a TypeError exception with the message size must be an integerif size is less than 0, raise a ValueError exception with the message size must be >= 0if size is a float and is less than 0, raise a TypeError exception with the message size must be an integerYou are not allowed to import any module
Write a function that reads a number N (1 to 100) and prints all the perfect squares upto N.
completing the square
What will be the output of the below C program?#include <stdio.h>#define square(x) x*x int main(){int x; x = 36/square(6); printf("%d", x); getchar(); return 0; }1636None of the above
How do you print the representation of a variable?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.