Skip to main content

Section 1.3 HW2

Exercises Exercises

1. Flip Sign and Add Three.

Write a short program (not a function definition) that will get a decimal value x (type float) from standard input and print a message regarding the value of x and the value of 3 - x.
For example:
Input Result
5.5
x is 5.5 and 3 - x is -2.5
-8.1
x is -8.1 and 3 - x is 11.1
0.12
x is 0.12 and 3 - x is 2.88

2. Calculate the Ratio as a Percentage.

Write the code needed to get two float values from standard input, A and B. Calculate A as a percentage of B, rounded to one decimal place. For example, if A is 25.0 and B is 75.0, A is about 33.3% of B since 25.0 / 75.0 = 1 / 3 = .3333333... (multiply the ratio by 100 to get the percentage).
Print a message to report the result (using the same format as shown in the examples below).
For example:
Input Result
5
15
5.0 is about 33.3 percent of 15.0
12.5
28.0
12.5 is about 44.6 percent of 28.0
2.52
8.1
2.52 is about 31.1 percent of 8.1

3. Rounded Square Root.

Write the code needed to get an integer value from standard input, calculate the square root of the value rounded to the nearest hundredth, then print a message with the result.
NOTE: You may assume that the input value will not be negative.
Reminder:
  • Start by importing the math module.
  • Use the function math.sqrt to calculate a square root.
For example:
Input Result
the square root of 11 is about 3.32
the square root of 95 is about 9.75
the square root of 3 is about 1.73

4. Round Float.

Write the code needed to get two numbers from standard input:
  • a decimal value (i.e., a number with a decimal point, which will be read as Python type float)
  • an integer value
Assign the decimal value to the variable decimal and the integer value to the variable precision (or other reasonable names).Finally, round the value of decimal to the number of places after the decimal point indicated by precision, and print the result.
For example:
Input Result
5.555
1
5.6
-9.87437
3
-9.874
0.900001
0
1.0
-42.4444
1
-42.4

5. Integer Division and Remainder.

Write the code needed to get two integer values from standard input, assign the first integer to the variable dividend, and assign the second integer to the variable divisor. Calculate two things and print the results:
  1. Use integer division (floor division) to calculate how many times the divisor will go into the dividend, excluding the remainder.
  2. Use the remainder operator to calculate the integer remainder that will be left over when the dividend is divided by the divisor.
For example:
Input Result
10
7
7 goes into 10 1 times with 3 left over
100
35
35 goes into 100 2 times with 30 left over
10
5
5 goes into 10 2 times with 0 left over

6. Bookcases.

You are planning to add bookcases to a wall in your living room, but you don’t know how many bookcases to buy. You decide to write a Python function, get_num_bookcases, to help. Your function needs to do the following:
  1. Take parameters for the width of the wall (an integer number of feet) and the width of a bookcase (an integer number of inches). (Your function does not read input from the keyboard; it gets its input through named parameters.)
  2. Calculate how many whole bookcases will fit on the wall using modular arithmetic (use div and/or mod, represented by the operators // and %, as needed to solve the problem).
  3. Returnthe number of bookcases.
For example:
Test Result
print(get_num_bookcases(20, 50))
print(get_num_bookcases(8, 15))
print(get_num_bookcases(3, 40))

7. Detect Even/Odd.

Write a function with no parameters, even_or_odd()
Specifications:
  • read an integer from standard input
  • return 0 if the integer is even, 1 if the integer is odd
  • do not use a conditional ("if") statement, just use modular arithmetic (such as // or %)
  • do not print the result, return it
Hint: What is the remainder when you divide an even number by 2?Β  What is the remainder when you divide an odd number by 2?
For example:
Test Input Result
ans = even_or_odd()
print(ans)
ans = even_or_odd()
print(ans)
-14
ans = even_or_odd()
print(ans)
ans = even_or_odd()
print(ans)

8. Spam, N Times.

Write a function, say_spam(⁠⁠n); it takes an integer parameter n and prints "Spam!" n times in response.
You should assume n > 0 for this function.
Hint: Use a simple for loop with a print statement in the body of the loop.
For example:
Test Result
say_spam(1)
Spam!
say_spam(2)
Spam!
Spam!
say_spam(5)
Spam!
Spam!
Spam!
Spam!
Spam!

9. Print Integers Less Than n.

Write a function, print_integers_less_than(⁠n), which takes an integer parameter n and prints each integer k which is at least 0 and is less than n, in ascending order.
Hint: use a simple for loop.
For example:
Test Result
print_integers_less_than(2)
0
1
print_integers_less_than(5)
0
1
2
3
4
print_integers_less_than(-3)

10. Print Perfect Cubes.

Write a function, print_perfect_cubes(n), that takes an integer parameter n and prints the perfect cubes starting from \(0^3 = 0\) and ending with \(n^3\text{.}\) When n is negative, the function prints nothing; but do not check for this condition with an "if" statement, it is unnecessary; use a for loop that will automatically print nothing when \(n < 0\text{.}\)
Hint for choosing the right range for your for loop: In the general case, when n is not negative, your function will need to print n+1 different values.
Hint for printing the first part of the output: Convert n to a string and then concatenate that string with "^3"; assign the result to a variable, the_cube. Make the_cube the first argument in your print function call.
For example:
Test Result
print_perfect_cubes(2)
0^3 = 0
1^3 = 1
2^3 = 8
print_perfect_cubes(5)
0^3 = 0
1^3 = 1
2^3 = 8
3^3 = 27
4^3 = 64
5^3 = 125
print_perfect_cubes(-10)
You have attempted of activities on this page.