Part II - Python Graphics Solve the problem below and save it in a file called h
ID: 3669723 • Letter: P
Question
Part II - Python Graphics Solve the problem below and save it in a file called homeworkl-chart.py. At the top of your homeworkl-chart.py file include the following information in a comment, with each item on a separate line: » first and last name · Stony Brook ID# e the course (CSE 101) the course (CSE 101) · your lab section number (a number from 8 to 13) * the assignment name and number (Homework 1) Problem Description You will write a program that displays a column chart for a student's expenses for a semester. Using pop-up windows, the program should begin by prompting for four integers, in this order: » Rent » Utilities » Food Fun You may assume that every value is in the range 0 through 500, inclusive, and that all four values are unique. To get input in a pop-up window requires a few steps. First, let's assume that your turtle is named "bob": from turtle import * bob = Turtle ( ) Now, we need to have access to the window itself so that we can pop open smaller windows inside of it. Here's how we set up ourselves to do this: screen = bob.getscreen() For the rest of this program we will use this screen variable to get input from the user, so you only need to include that line of code once in your program. To create a pop-up window we have to give two pieces of information to the Python interpreter: 1, a short title for the window and 2. a prompt message that tells the user what to type in For example, here is how we might ask the user to type in the rent, storing the user's response in a variable called rent:Explanation / Answer
from turtle import *
tur = Turtle()
screen = tur.getscreen()
rent = screen.numinput("Student Expenses", "Enter Rent:", 0, minval=0, maxval=500)
utilities = screen.numinput("Student Expenses", "Enter expenses for Utilities:", 0, minval=0, maxval=500)
food = screen.numinput("Student Expenses", "Enter expenses for food:", 0, minval=0, maxval=500)
fun = screen.numinput("Student Expenses", "Enter expenses for fun:", 0, minval=0, maxval=500)
def drawBar(t, height):
# """ Get turtle t to draw one bar, of height. """
t.begin_fill() # start filling this shape
t.left(90)
t.forward(height)
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
t.end_fill() # stop filling this shape
xs = [rent, utilities, food, fun] # here is the data
ys = ["Rent", "Utilities","Food","Fun"]
maxheight = max(xs)
numbars = len(xs)
border = 10
screen.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
screen.bgcolor("lightgreen")
# create tess and set some attributes
tur.color("blue")
tur.fillcolor("red")
tur.pensize(2)
for a in xs:
drawBar(tur, a)
screen.exitonclick()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.