Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON: can someone help me with my code? Step 3: Creating a Bar Graph In this step you will create a bar graph using the

PYTHON: can someone help me with my code?

Step 3: Creating a Bar Graph

In this step you will create a bar graph using the return value from the seq3np1 function as the height of each bar. You will use the loop code that you wrote in the previous step, but in order to make this work, please pay attention to the directions specified below to avoid errors in your code. Where you write the code will make a difference.

At the top of your code file:

1. Setup your turtle and screen objects.

2. Define a function called draw_bar with the formal parameters myturtle and height . The function will draw one bar in a bar chart

a. Formal Parameters: myturtle - the turtle object that will draw each bar in the chart, height - the height of the bar in the chart

b. Repeat the following steps two times (use a for loop)

1. Move forward 10 units 2. Turn left 90 degrees 3. Move forward height units 4. Turn left 90 degrees

Before the for loop (that is already created at the bottom):

1. Move the turtle to the location (-200, 0) without drawing a line.

2. Set the speed to 0.

In the body of the for -loop (at the bottom):

1. Define a variable called bar_height and assign it the value returned from seq3np1 .The value returned from seq3np1 is the count, which is also the height of the bar.

2. Call draw_bar passing in the necessary values. The variable bar_height represents the height of each bar in the graph.

3. Move the turtle forward 10 units.

MY CODE:

import turtle my_turtle = turtle.Turtle() def draw_bar (my_turtle, height): height = bar_height

for b in range (2): my_turtle.forward(10) my_turtle.left(90) my_turtle.forward(height) my_turtle.left(90)

def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1.""" count = 0 while n != 1: count +=1 if n % 2 == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1 return count my_turtle.penup() my_turtle.goto(-200,0) my_turtle.pendown() my_turtle.speed(0) def par():

for start in range (1,51): variable = seq3np1(start) bar_height = variable draw_bar(my_turtle, bar_height) my_turtle.forward(10) print("The start value =", start, "; the return value =", variable) par()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions

Question

Knowledge of process documentation (process flow charting)

Answered: 1 week ago