ICS 31, Summer Session 10-WK, 2020
HW 1
Do the following exercises and place all of the code in a single file. Be sure to include a comment of the form # Problem ? at the beginning of the solution to each problem, where the ? is replaced with the problem number. Also, include a comment of the form # lastName, firstName which includes your name at the top of the file.
Problem 1 Write a program which prompts the user for a radius and a height and then proceeds to print the volume of the cylinder. Use 3.14 for the value of pi.
Example Output (user input is italicized) Enter a radius: 2
Enter a height: 4
The volume is 50.24
Problem 2 Write a Python program which accepts the user’s first and last name and prints the first name two times, separated by spaces and the last name three times without spaces.
Example Output (user input is italicized) Enter a first name: Bob
Enter a last name: Jacobs
Bob Bob JacobsJacobsJacobs
Problem 3 Write a program which accepts two sequences of numbers from the user, separated by commas, and generates coordinates with those numbers.
Example Output (user input is italicized)
Enter the first sequence of numbers separated by commas: 0,1,2 Enter the second sequence of numbers separated by commas: 0,1,2 Coordinates: [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)]
Problem 4 Write a program which accepts a number and prints the cubic root of the absolute value of the number.
Example Output (user input is italicized) Enter a number: -27
Result: 3
Problem 5 Write a program which generates a random number between 1-100 and prompts the user to guess the number until it is correct
Example Output (user input is italicized) Enter a number: 50
The number is greater than 50 Enter a number: 90
The number is less than 90 Enter a number: 75
The number is greater than 75 Enter a number: 85
The number is less than 85
Enter a number: 82 Congratulations! The number is 82!
Problem 7 Write Python expressions that correspond to each of the following statements:
a. The sum of the five odd integers from 10-20
b. 66 to the 4th power
c. A moving anteater has mass of 40 kg and a velocity (speed) of 10 meters per second.
Compute its kinetic energy using this formula: 1⁄2 times the mass times the velocity squared.
Problem 8 Assume that the variable test_scores is a string consisting of scores on a quiz. Each score is an integer between 0 and 9. Assume that test_scores is assigned as follows.
test_scores = ‘2 5 6 9 3 8 2 6 5 9 5 6 6 2 9 0 9 8 8 7’
Using the variable test_scores and the indexing operators, write four separate expressions whose values are each of the following:
1. Quiz score for the 2nd student
2. Quiz score for the 14th student
3. Quiz score for the 8th student plus 9th student
4. Quiz score for the 19th student subtracted by the 3rd student
Problem 9 Evaluate the following assignment statement:
>>> s = ‘computerscience’
For each of the following, write a boolean expression that represents the English statement:
a. The last character of string s is ‘b’
b. The first character of string s is ‘c’
c. There are three ‘e’ in string s
d. The length of string s is 16
Problem 10 Write a boolean expression corresponding to each of the following statements:
a. The type of a float multiplied by an int is an int
b. Multiplying list s = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] by 3 will result in a length of 15
c. The remainder when 20 is divided by 5 is less than or equal to 2
d. Five is a prime number and four is an odd number
e. The number 62 is type float
Problem 11 Execute the following assignment statement: >>> s = ‘abcdefghijklmnopqrstuvwxyz’
Using only string concatenation and the indexing operator on the string s, write Python expressions that result in the following:
a. ‘harris’
b. ‘lab’
c. ‘new’
d. ‘mmmyw’