Question

For Python: (Use # comments to explain each line) Write code that prints an octagon shape...

For Python: (Use # comments to explain each line)

Write code that prints an octagon shape using stars, by prompting the user for the octagon side length.  

For example: "How long should each side of the octagon be? "

Then takes this input, and prints an octagon in stars using the user input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

#split the octogon into 3 parts
length=int(input("How long should each side of the octagon be?")) #taking input from user
for i in range(length-1): #loop for upper part
print(' ' * (length-i-1) + '*' * (length+(i*2))) #no of spaces and no of stars printed
for i in range(length): #loop for middle part
print('*' * (length*3-2)) #no of stars printed here no spaces
for i in range(length-1,0,-1): #loop for lower part
print(' ' * (length-i) + '*' * (length+(i*2)-2)) #no of spaces and no of stars printed

Screen shot:-

Add a comment
Know the answer?
Add Answer to:
For Python: (Use # comments to explain each line) Write code that prints an octagon shape...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Study guide Intro to PYTHON questions: 25) Write the code that will generate this output using...

    Study guide Intro to PYTHON questions: 25) Write the code that will generate this output using only while loops and if statements (no string multiplication). ###AAAAAAAA### ###BBBBBBBB### ###CCCCCCCC### ###AAAAAAAA### ###BBBBBBBB### ###CCCCCCCC### 26) Write code that asks the user for a size and prints a triangle of stars of that size that increases by two at each level. You can only use one while loop and any number of variables and counters you want, but you are limited to these tools....

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing...

    Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing the area of the shape. You can assume that all the sides are equal in length. B. Implement the Comparable interface in the Circle and Rectangle from Question2 comparing the area of each shape. (use the same classes in Question 2) C. Also add the overloaded toString() method to each class that prints a summary of the object. Then, in another class, create an...

  • "Write a python program that gets a line of text from the user and prints the...

    "Write a python program that gets a line of text from the user and prints the line 5 times, with line numbers 1 to 5 attached in the beginning of each line." I do not know how to add the numbers to each line. What should I do?

  • write a script named word_counts.sh that prints out how many words are on each line of...

    write a script named word_counts.sh that prints out how many words are on each line of standard input, as well as the total number of words at the end. The script should take no arguments. Instead, the script must work by reading every line of standard input (using a while loop) and counting the number of words on each line separately. The script is intended to work with standard input coming from a pipe, which will most often come from...

  • Python programming Question 1: Write a script with a for loop that prints each character in...

    Python programming Question 1: Write a script with a for loop that prints each character in your first name written as first initial capital and other small, followed by its ASCII value on the screen.  Use appropriate functions to get character value to ASCII code. (Hint: Functions discussed in lectures)  For example, the output of each iteration should be as -- for ‘A’, it should print A followed by its ASCII value. Question 2: Write a script that...

  • In Jgrasp(Java) Thanks! This solution must use methods. It is recommended to use methods for this...

    In Jgrasp(Java) Thanks! This solution must use methods. It is recommended to use methods for this solution. Write a program that reads an integer and displays, using asterisks, a filled and hollow square, placed next to each other. For example, if the side length is 5. the program should display: ***** ***** You can assume that the user will enter a side length that is at least 2. If user enters a number smaller than 2, and your program has...

  • Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

    Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence...

  • Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

    Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence...

  • Write a python code that takes in an number 0-9 and prints out the word of...

    Write a python code that takes in an number 0-9 and prints out the word of the number. For example 1 would print out one. Below is the skeleton of the code that needs to be filled in. def num2string(num): """ Takes as input a number, num, and returns the corresponding name as a string. Examples: num2string(0) returns "zero", num2string(1)returns "one" Assumes that input is an integer ranging from 0 to 9 """ numString = "" ################################### ### FILL IN...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT