MONT 105S, Spring 2009

Home | | Course Schedule | | Assignments | | Lecture Notes

Laboratory 3
Due at the end of today's class

Problem #1.
Write a Python program that uses a turtle to draw a flower. The program should ask the user for the number of petals and should use a while loop to draw that number of petals. The petals should be equally spaced around a circle. In other words, if the user enters 3, the program should draw 3 petals, spaced at intervals of 360/3=120degrees.

You should use the following code to draw a single petal:

    yertle.forward(50)
    yertle.left(20)
    yertle.forward(50)
    yertle.left(160)
    yertle.forward(50)
    yertle.left(20)
    yertle.forward(50)
    yertle.left(160)

Below is a picture of a flower with 8 petals.

You may use the program skeleton given below:

from turtle import Pen as Turtle
yertle = Turtle( )

petals = input("How many petals would you like? ")
turn = 360/petals            #Amount to turn after drawing each petal

count = 0                    #variable to count how many petals have been drawn.

#Add your own code here to write a while loop to draw the petals.
#The loop should draw a single petal each time it is executed.
#It should then turn the turtle the correct amount to be ready to draw the next petal.
#it should also increment count by 1.
#The loop should continue as long as count is less than the desired number of petals


Problem #2.
Write a program to play the game of "Guess my number". The program should choose a number between 1 and 10. For each incorrect guess the user makes, it should tell the user whether their guess was high or low and ask for another guess. The program should terminate when the user guesses the correct number or when the user has made 4 incorrect guesses (whichever comes first).

You should use the program skeleton given below:

import random

myNumber = random.randrange(10) + 1

guess = input("Guess a number between 1 and 10: ")
guessCount = 1

#Use a while loop here.
#The loop should continue as long as the guess is incorrect and the guessCount is less than 4.
#In the body of the loop, the program should print "Too low" if the guess is less than myNumber.
#        Otherwise the program should print "Too high".
#The body of the loop should always ask for and input another guess
#       (after indicating "Too high" or "Too low")
#The body of the loop should always increment the guessCount by 1.

#Write a conditional here (outside the loop!) to give the appropriate response to the user.
#If the user guessed the number, tell them they guessed it.
#If the user failed to guess the number, tell them they failed in 4 tries.

The following is an example of the output of running the program:

Sample run 1:

Guess a number between 1 and 10: 5
Too low.  Guess again!
Guess a number between 1 and 10: 8
Too high.  Guess again!
Guess a number between 1 and 10: 6
You guessed it!

Sample run 2:

Guess a number between 1 and 10: 3
Too low.  Guess again!
Guess a number between 1 and 10: 9
Too high.  Guess again!
Guess a number between 1 and 10: 5
Too low.  Guess again!
Guess a number between 1 and 10: 7
Sorry.  You failed to guess in 4 tries.


What To Turn In.
A printed listing of each lab program. Be sure that your name is in the program prologue's comment section of EACH program.

Reminder.
Be sure to save a copy of each program on your P:\ drive.
You are responsible for keeping a copy of every program until the graded assignment is handed back.