Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python3 A 3x3 magic square is a 3 by 3 square arrangement of digits between 1 and 9 (without duplicates) such that the sum of
Python3
A 3x3 magic square is a 3 by 3 square arrangement of digits between 1 and 9 (without duplicates) such that the sum of any 3 digits horizontally, vertically, or diagonally equals 15. You can read more about these on the Wikipedia page for magic squares . Your program will take a potential magic square from user input and validate whether the input is a magic square. Take the input as three separate lines from the user (you can use a loop to get three lines one after the other. Using input() without an argument will let the user input another line without any printed prompt.) Each line should consist of three numerical characters. Once all three lines have been entered the program will evaluate whether or not the three rows form a magic square and report the result. For example, in this case, the numbers do add up to 15 in all 8 directions (three horizontal rows, three vertical columns, and two corner-to-corner diagonals): $ python magic_square_validator.py Enter a magic number 492 357 816 This is a magic square! If the input values do not add up to 15 in all 8 directions, your program will report that the input square is not a magic square. $ python magic_square_validator.py Enter a magic number 123 456 789 Not a magic square! Consider what would need to change to generalize this validator to squares of arbitrary sizes (you don't need to code this for the lab, but please think about what you would do differently). Name the program magic_square_validator.py. Write a program called christmas_tree.py that uses a for loop to draw a Christmas tree along the lines of the following: __ The left edge of the tree is made up of characters and the right edge is made up of characters, the base (between the two edge characters) is made up of underscores), and the top of the tree is an asterisk. Take an odd-valued input of 3 or greater from the user representing the width if the base of the tree. For the example above, the value would be 7. If the user inputs an even number, give them another chance to input a different (odd) number. An input of 3 would yield the following tree: XV An input of 9 would yield: /_ Similarly to the examples above, write a short script called circle.py that takes a value from the command line and prints out a circle with the radius corresponding to that value. For example, for the value 20, it would print something like this output (Okay, yes, this really renders more as an oval, because vertical spacing makes the characters we're using as pixels taller than they are wide. But the height and the width are the same number of characters, so we'll call it a circle): $ python circle.py 20 0000000000000 00000000000000000 000000000000000000000 00000000000000000000000 000000000000000000000000000 0000000000 00000000 0000000000 0000000 oooooood 000000000000 00000 00000000000000 ooooo 000000000000000000 0000000000 0000000000000 00000 0000000000000 00000000ood 000000 00000000 0000000000000000 0000000000000 00000 0000000000000 00000 00000000oC 00000 000000000 0000000000000 0000000000000 0000000000000000 100000000000 0000000000000 00000000 0000000000000 00000000 ooooooooo ooooo 0000 000000 000000000000 0000000 00000000000oood 0000000 0000000000000000000000000000000 00000000000000000000000000000 000000000000000000000000000 00000000000000000000000 000000000000000000000 00000000000000000 0000000000000 In order to determine where to draw o s and where to draw spaces, you'll want to think of each character's position in terms of an x and y position in a grid. The grid will be twice the radius high and twice the radius wide. Use the Pythagorean theorem to determine whether the distance of each character to the center of the grid is less than the value of the radius. If the distance is less than the radius, then the o character will be rendered in that position, otherwise the (empty space) will be rendered in that position. You can use the sqrt function from the math library (you must import math). Another function you might find useful is the abs function (absolute value). This function is built into Python's standard library, so you don't need to import anything to use itStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started