Difference between revisions of "Turtle - Graphing Calculator"

From WLCS
 
(4 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
# Name your file: '''turtleGrapher.py''':
 
# Name your file: '''turtleGrapher.py''':
 
# Set your turtle's speed to the fastest: '''speed(0)'''
 
# Set your turtle's speed to the fastest: '''speed(0)'''
# Prompt the user for the width of the window and store in a variable named '''width'''
+
# Use '''numinput()''' to prompt the user for the width of the window and store in a variable named '''width'''
# Prompt the user for the height of the window and store in a variable named '''height'''
+
# Use '''numinput()''' to prompt the user for the height of the window and store in a variable named '''height'''
 
# Change the dimensions of the window by using '''setup(width, height)''' and then '''screensize(width, height)'''
 
# Change the dimensions of the window by using '''setup(width, height)''' and then '''screensize(width, height)'''
# Draw a line that represents the x-axis
+
# Draw a line that represents the x-axis through the middle of the screen
# Draw a line that represents the y-axis
+
# Draw a line that represents the y-axis through the middle of the screen
# Using textinput(), prompt the user to enter an equation (e.g. 2*x+3) and store it in a variable: '''eq'''
+
# Use '''textinput()''' to prompt the user to enter an equation (e.g. 2*x+3) and store it in a variable: '''eq'''
# Write a while loop where x starts at -width/2 and goes all the way through width/2
+
# Write a while loop where x starts at '''-width/2''' and goes all the way through '''width/2'''
 
## Inside the while loop, use eval to get the y-value of the equation: '''y = eval(eq)'''
 
## Inside the while loop, use eval to get the y-value of the equation: '''y = eval(eq)'''
## Tell the turtle to goto(x, y)
+
## Tell the turtle to '''goto(x, y)'''
 +
## Make sure your the turtle's pen is down so that the graph is drawn
 
# Tell the turtle window to exit when it is clicked
 
# Tell the turtle window to exit when it is clicked
 +
 +
'''Advanced: (Optional)'''
 +
* Once you have the basic calculator working, try to implement a zoom feature that draws the equation bigger
 +
* Draw the tick marks on the x and y-axes

Latest revision as of 14:19, 7 November 2012

Objectives:

  • You will practice creating while loops
  • You will integrate loops into drawing graphics with Python's turtle
  • You will create a basic graphing calculator

Resources:

Directions:

  1. Name your file: turtleGrapher.py:
  2. Set your turtle's speed to the fastest: speed(0)
  3. Use numinput() to prompt the user for the width of the window and store in a variable named width
  4. Use numinput() to prompt the user for the height of the window and store in a variable named height
  5. Change the dimensions of the window by using setup(width, height) and then screensize(width, height)
  6. Draw a line that represents the x-axis through the middle of the screen
  7. Draw a line that represents the y-axis through the middle of the screen
  8. Use textinput() to prompt the user to enter an equation (e.g. 2*x+3) and store it in a variable: eq
  9. Write a while loop where x starts at -width/2 and goes all the way through width/2
    1. Inside the while loop, use eval to get the y-value of the equation: y = eval(eq)
    2. Tell the turtle to goto(x, y)
    3. Make sure your the turtle's pen is down so that the graph is drawn
  10. Tell the turtle window to exit when it is clicked

Advanced: (Optional)

  • Once you have the basic calculator working, try to implement a zoom feature that draws the equation bigger
  • Draw the tick marks on the x and y-axes