Download print "The area of the circle with radius", r `is`, area

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computer simulation wikipedia , lookup

Types of artificial neural networks wikipedia , lookup

SahysMod wikipedia , lookup

K-nearest neighbors algorithm wikipedia , lookup

Backpropagation wikipedia , lookup

Control system wikipedia , lookup

Transcript
Math 110 CS Homework 2 Solutions
1. What is the problem with the Python program below? Check your answer in IDLE.
import math
radius = 7
area = pi * radius**2
print area
Solution: The problem here occurs in the third statement. In order to use the constant pi from the math
module you must say math.pi. Entering these lines in Python will yield a runtime error that states the
“name pi is not defined”.
2. Find all of the errors (syntax, run-time, and logic) in the Python program below:
import Math
radius = input("Enter the radius of a circle:)
area = math.Pi * Radius*2
print "The area of the circle with radius", r 'is', area
Solution:
There is an error in Line 1 (Math should not be capitalized – it should say import math). This is a syntax
error.
There is an error in Line 2 (The ending quotation mark is missing to indicate the end of the string argument
to the input function. This is a syntax error.
There are several errors in Line 3 (one syntax error, one runtime error, and one logic error). The syntax
error is the capitalized p in math.Pi – it should be math.pi. The runtime error is the variable name Radius –
this name is not defined, the name radius (with a lower case r) is defined in Line 2 – Python is case sensitive,
so be careful with capitalization. The logic error is that we are computing the area by multiplying π times the
radius of the circle times 2 (2𝜋𝑟), but the actual formula for the area of a circle is 𝜋 × 𝑟 2 .
There are two errors in Line 4 : r is not defined (we called the input radius in Line 2). Note that the use of
single quotes (‘ ‘) to indicate text is fine. The second error is that there needs to be a comma between all of
the things to be output (so need a comma between r and ‘is’).
3. Write a Python program that prompts a user to enter a distance in yards, convert the distance to miles, and then
prints the output in sentence form. Conversion: 1760 yards = 1 mile. To check your program, try to convert 200
yards. The answer should be 0.1136 miles.
4. Write a Python program that prompts a user to enter a volume in gallons, converts it to cups, and then prints
the output in sentence form. Conversion information: 1 quart = 4 cups and 1 gallon = 4 quarts. To check your
program, try to convert 2.2 gallons. The answer should be 35.2 cups.
5. Optional Homework (A little harder than the above programs): Relative humidity is a term used to describe the
amount of water vapor in a mixture of air and water vapor. It depends on the temperature of the air as well as
the dew point (the temperature to which a given parcel of humid air must be cooled for water vapor to
condense into water). Generally, days with high relative humidity are considered uncomfortable (although
having very low relative humidity can lead to uncomfortable weather as well). Write a Python program that asks
the user to enter a temperature and dew point (both in degrees Celsius), uses them to compute relative
humidity (which has units of percent) using the following formula
112 − 0.1𝑇 + 𝐷 8
𝑅 = 100 �
�
112 + 0.9𝑇
(where T = temperature in degrees Celsius and D = dew point in degrees Celsius), and prints the output (rounded
to 2 decimal places) in sentence form. To check your work, for a temperature of 25°C and a dew point of 20°C,
the relative humidity is 73.85%.