Question

In: Computer Science

Can somebody describe what this following code does step by step. import turtle turtle.penup() x =...

Can somebody describe what this following code does step by step.

import turtle
turtle.penup()


x = int(input("Enter x coordinate of top-left corner: "))
y = int(input("Enter y coordinate of top-left corner: "))
side = int(input("Enter side of the largest square: "))
if side >= 100:
gap = int(input("Enter gap between squares: "))
if gap < side:
turtle.goto(x,y)
turtle.pendown()
while True:
for i in range(4):
turtle.forward(side)
turtle.right(90)
side -=gap
turtle.penup()
x += gap
y -= gap
turtle.goto(x,y)
turtle.pendown()

if side<0:
exit(1)

Solutions

Expert Solution

Step by step explanation of given python code

import turtle - turtle() is a graphical library in python that gives command to a turtle to draw something over a window.

turtle.penup() - this command will tell turtle to move around the screen without drawing anything.

x = int(input("Enter x coordinate of top-left corner: "))

This will create a variable named x and store x coordinate of top-left corner of screen entered by the user. int will perform conversion as default type in python is string.

y = int(input("Enter y coordinate of top-left corner: "))

This will create a variable named y and store y coordinate of top-left corner of screen entered by the user. int will perform conversion as default type in python is string.

side = int(input("Enter side of the largest square: "))

This will create a variable named side and store side of the square entered by the user. int will perform conversion as default type in python is string

if side >= 100:

If condition will begin to check whether side is greater than 100 or not.

gap = int(input("Enter gap between squares: "))

gap is a variable that will store value of gal between squares.

if gap < side:

If gap is less than side next statement will be executed.

turtle.goto(x,y)

This command will tell the turtle to move to (x,y) coordinate position entered by the user.

turtle.pendown()

this command will tell turtle to move around the screen by drawing a line.

while True:

Until whilw condition is true loop will continue to execute next statements.

for i in range(4):

For loop will start from variable i to range 4

turtle.forward(side)

Turtle will move forward few pixels taking value of side.

turtle.right(90)

Turtle will turn to its right by 90 degrees.

side -=gap

It will compute side= side - gap

turtle.penup()

Turtle will again move without drawing anything.

x += gap

It will increase the value of x = x + gap

y -= gap

It will decrease the value of y coordinate y=y- gap

turtle.goto(x,y)

Turtle will now move to changed values of (x,y) coordinates

turtle.pendown()

Turtle will not draw anything.

if side<0:

If value of side is less than 0 than turtle will exit.

exit(1)

Program terminates.


Related Solutions

what is the following (ARDUINO): -Libraries are code that allow -Lcd. setCursor (x,y) -Where to Import...
what is the following (ARDUINO): -Libraries are code that allow -Lcd. setCursor (x,y) -Where to Import a library into a given code -Digital pins can be used to
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at ", datetime.datetime.now()) print(getpass.getuser())
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
What would the Python code be to create the following working program using turtle: -Make 4...
What would the Python code be to create the following working program using turtle: -Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists. -Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100) -Make the green turtles start at a different randomized location somewhere on the right side of the screen within the...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
What is wrong with the following code? Give two effective ways to fix it. 1 import...
What is wrong with the following code? Give two effective ways to fix it. 1 import javax.swing.*; 2 import java.awt.event.*; 3 4 public class H2ClassJ extends JFrame { 5    public static final long serialVersionUID = 22; 6 7 public H2ClassJ () { 8      addMouseListener (new MouseListener () { 9    public void mouseClicked (MouseEvent e) {} 10     }); 11   } // end constructor 12 13 } // end class H2ClassJ
. Describe what is meant by a company’s code of ethics. . What is does stakeholder...
. Describe what is meant by a company’s code of ethics. . What is does stakeholder theory say that social responsibility means? . What is sustainability? What does it purport? . What is green marketing and why do you think is it important? . What is cause-related marketing? Why is it popular in business today?
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT