Exercises2 2
Exercises2 2
Exercises2 2
Enter an integer: 23
23 is odd.
1. It is commonly said that one human year is equivalent to 7 dog years. However this simple conversion
fails to recognize that dogs reach adulthood in approximately two years. As a result, some people
believe that it is better to count each of the first two human years as 10.5 dog years, and then count
each additional human year as 4 dog years.
Write a program that implements the conversion from human years to dog years described in the previous
paragraph. Ensure that your program works correctly for conversions of less than two human years and for
conversions of two or more human years. Your program should display an appropriate error message if the
user enters a negative number.
1. In this exercise you will create a program that reads a letter of the alphabet from the user. If the user
enters a, e, i, o or u then your program should display a message indicating that the entered letter is a
vowel. If the user enters y then your program should display a message indicating that sometimes y is a
vowel, and sometimes y is a consonant. Otherwise your program should display a message indicating
that the letter is a consonant.
Enter a letter: r
It's a consonant.
TASK
Write a program that determines the name of a shape from its number of sides. Read the number of sides
from the user and then report the appropriate name as part of a meaningful message. Your program should
support shapes with anywhere from 3 up to (and including) 10 sides. If a number of sides outside of this
range is entered then your program should display an appropriate error message.
TASK A triangle can be classified based on the lengths of its sides as equilateral, isosceles or scalene. All
three sides of an equilateral triangle have the same length. An isosceles triangle has two sides that are the
same length, and a third side that is a different length. If all of the sides have different lengths then the
triangle is scalene. Write a program that reads the lengths of the three sides of a triangle from the user.
Then display a message that states the triangle’s type.
TASK The year is divided into four seasons: spring, summer, fall (or autumn) and winter. While the exact
dates that the seasons change vary a little bit from year to year because of the way that the calendar is
constructed, we will use the following dates for this exercise: Season First Day Spring March 20 Summer
June 21 Fall September 22 Winter December 21 Create a program that reads a month and day from the
user. The user will enter the name of the month as a string, followed by the day within the month as an
integer. Then your program should display the season associated with the date that was entered.
TASK The following table lists an octave of music notes, beginning with middle C, along with their
frequencies. Note Frequency (Hz) C4 261.63 D4 293.66 E4 329.63 F4 349.23 G4 392.00 A4 440.00 B4 493.88
Begin by writing a program that reads the name of a note from the user and displays the note’s frequency.
Your program should support all of the notes listed previously. Once you have your program working
correctly for the notes listed previously you should add support for all of the notes from C0 to C8. While this
could be done by adding many additional cases to your if statement, such a solution is cumbersome,
inelegant and unacceptable for the purposes of this exercise. Instead, you should exploit the relationship
between notes in adjacent octaves. In particular, the frequency of any note in octave n is half the frequency
of the corresponding note in octave n + 1. By using this relationship, you should be able to add support
for the additional notes without adding additional cases to your if statement.
1. The following table lists the sound level in decibels for several common noises.
Noise Decibel Level Jackhammer 130 dB Gas Lawnmower 106 dB Alarm Clock 70 dB Quite Room 40 dB
Write a program that reads a sound level in decibels from the user. If the user enters a decibel level that
matches one of the noises in the table then your program should display a message containing only that
noise. If the user enters a number of decibels between the noises listed then your program should display a
message indicating which noises the value is between. Ensure that your program also generates reasonable
output for a value smaller than the quietest noise in the table, and for a value larger than the loudest noise
in the table.
In [ ]:
1. The year is divided into four seasons: spring, summer, fall (or autumn) and winter. While the exact dates
that the seasons change vary a little bit from year to year because of the way that the calendar is
constructed, we will use the following dates for this exercise:
Season First Day Spring March 20 Summer June 21 Fall September 22 Winter December 21 Create a
program that reads a month and day from the user. The user will enter the name of the month as a string,
followed by the day within the month as an integer. Then your program should display the season
associated with the date that was entered.
In [ ]:
1. The Chinese zodiac assigns animals to years in a 12 year cycle. One 12 year cycle is shown in the table
below. The pattern repeats from there, with 2012 being another year of the dragon, and 1999 being
another year of the hare.
Year Animal 2000 Dragon 2001 Snake 2002 Horse 2003 Sheep 2004 Monkey 2005 Rooster 2006 Dog 2007
Pig 2008 Rat 2009 Ox 2010 Tiger 2011 Hare Write a program that reads a year from the user and displays
the animal associated with that year. Your program should work correctly for any year greater than or equal
to zero, not just the ones listed in the table.
1. A roulette wheel has 38 spaces on it. Of these spaces, 18 are black, 18 are red, and two are green. The
green spaces are numbered 0 and 00. The red spaces are numbered 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21,
23, 25, 27, 30 32, 34 and 36. The remaining integers between 1 and 36 are used to number the black
spaces.
Many different bets can be placed in roulette. We will only consider the following subset of them in this
exercise:
Single number (1 to 36, 0, or 00) Red versus Black Odd versus Even (Note that 0 and 00 do not pay out for
even) 1 to 18 versus 19 to 36 Write a program that simulates a spin of a roulette wheel by using Python’s
random number generator. Display the number that was selected and all of the bets that must be payed. For
example, if 13 is selected then your program should display:
The spin resulted in 13... Pay 13 Pay Black Pay Odd Pay 1 to 18 If the simulation results in 0 or 00 then your
program should display Pay 0 or Pay 00 without any further output.
In [ ]:
1. In this exercise you will create a program that computes the average of a collection of values entered by
the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your
program should display an appropriate error message if the first value entered by the user is 0.
Hint: Because the 0 marks the end of the input it should not be included in the average.
In [ ]:
1. Write a program that displays a temperature conversion table for degrees Celsius and degrees
Fahrenheit. The table should include rows for all temperatures between 0 and 100 degrees Celsius that
are multiples of 10 degrees Celsius. Include appropriate headings on your columns. The formula for
converting between degrees Celsius and degrees Fahrenheit is as follows
In [ ]:
1. A particular zoo determines the price of admission based on the age of the guest. Guests 2 years of age
and less are admitted without charge. Children between 3 and 12 years of age cost
14.00.Seniorsaged65yearsandovercost 18.00. Admission for all other guests is $23.00.
Create a program that begins by reading the ages of all of the guests in a group from the user, with one age
entered on each line. The user will enter a blank line to indicate that there are no more guests in the group.
Then your program should display the admission cost for the group with an appropriate message. The cost
should be displayed using two decimal places.
In [ ]:
1. The value of can be approximated by the following infinite series: $$ \pi \approx 3
In [ ]:
1. Fizz-Buzz is a game that is sometimes played by children to help them learn about division. The players
are commonly arranged in a circle so that the game can progress from player to player continually. The
starting player begins by saying one, and then play passes to the player to the left. Each subsequent
player is responsible for the next integer in sequence before play passes to the following player. On a
player’s turn they must either say their number or one of following substitutions:
If the player’s number is divisible by 3 then the player says fizz instead of their number. If the player’s
number is divisible by 5 then the player says buzz instead of their number. A player must say both fizz and
buzz for numbers that are divisible by both 3 and 5. Any player that fails to perform the correct substitution
or hesitates before answering is eliminated from the game. The last player remaining is the winner. Write a
program that displays the answers for the first 100 numbers in the Fizz- Buzz game. Each answer should be
displayed on its own line
In [ ]:
1. Write a program that implements Newton’s method to compute and display the square root of a
number, x, entered by the user. The algorithm for Newton’s method follows:
Read x from the user Initialize guess to x/2 While guess is not good enough do Update guess to be the
average of guess and x/guess When this algorithm completes, guess contains an approximation of the
square root of x. The quality of the approximation depends on how you define “good enough”. In the
author’s solution, guess was considered good enough when the absolute value of the difference between
guess∗guess and x was less than or equal to 10e-12 .
In [ ]:
1. A string is a palindrome if it is identical forward and backward. For example “anna”, “civic”, “level” and
“hannah” are all examples of palindromic words. Write a program that reads a string from the user and
uses a loop to determine whether or not it is a palindrome. Display the result, including a meaningful
output message.
In [ ]: