# 2.17. Exercises¶

1. Evaluate the following numerical expressions in your head, then use the active code window to check your results:

1. 5 ** 2

2. 9 * 5

3. 15 / 12

4. 12 / 15

5. 15 // 12

6. 12 // 15

7. 5 % 2

8. 9 % 5

9. 15 % 12

10. 12 % 15

11. 6 % 6

12. 0 % 7

1. 5 ** 2  = 25

2. 9 * 5 = 45

3. 15 / 12 = 1.25

4. 12 / 15 = 0.8

5. 15 // 12 = 1

6. 12 // 15 = 0

7. 5 % 2 = 1

8. 9 % 5 = 4

9. 15 % 12 = 3

10. 12 % 15 = 12

11. 6 % 6 = 0

12. 0 % 7 = 0

1. What is the order of the arithmetic operations in the following expression. Evaluate the expression by hand and then check your work.

1. Challenge: Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off.

1. It is possible to name the days 0 thru 6 where day 0 is Sunday and day 6 is Saturday. If you go on a wonderful holiday leaving on day number 3 (a Wednesday) and you return home after 10 nights you would return home on a Saturday (day 6). Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the number of day of the week you will return on.

1. Challenge: Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

1. Add parentheses to the expression 6 * 1 - 2 to change its value from 4 to -6.

1. Challenge: The formula for computing the final amount if one is earning compound interest is given on Wikipedia as Write a Python program that assigns the principal amount of 10000 to variable P, assign to n the value 12, and assign to r the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, t, that the money will be compounded for. Calculate and print the final amount after t years.

1. Write a program that will compute the area of a circle. Prompt the user to enter the radius and save it to avariable called radius. Print a nice message back to the user with the answer.

1. Challenge: Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle and store the values in variables called width and height. Print a nice message with the answer..

1. Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and store it in a variable called miles and the number of gallons used stored in a variable gallons. Print a nice message with the answer.

1. Challenge: Write a program that will convert degrees celsius to degrees fahrenheit.

1. Ask the user for the temperature in Fahrenheit and store it in a variable call deg_f. Calculate the equivalent temperature in degrees Celsius and store it in deg_c. Output a message to the user giving the temperature in Celsius.

1. Piece together the code so that a user is asked for two numbers, and then the sum of those two numbers is printed out.

1. Write a program that will convert gallons to liters. This program will also need to get input from a user to see how many gallons should be converted and the result should be printed to the user.

1. Write a program that will convert tablespoons to teaspoons. This program will also need to get input from a user to see how many tablespoons should be converted and the result should be printed to the user.

## 2.17.1. Contributed Exercises¶

Write a program that inputs the number of seconds, and returns the number of minutes and seconds. For example, “130” should return “2 minutes and 10 seconds”.
Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on a 24 hour clock when the alarm goes off.

The formula for computing the final amount if one is earning compound interest is given on Wikipedia as Write a Python program that assigns the principal amount of 10000 to variable P, assign to n the value 12, and assign to r the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, t, that the money will be compounded for. Calculate and print the final amount after t years.

Escribe un programa que calcule el consumo de un coche, medido en litros de gasolina por cada 100 kilómetros. preguntará al usuario el número de kilómetros recorridos y el número de litros consumidos, y mostrará un mensaje con el consumo del coche.
Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.
Escribe un programa que calcule el consumo de un coche, medido en litros de gasolina por cada 100 kilómetros y en kilómetros que se pueden recorrer con un litro de gasolina. Preguntará al usuario el número de kilómetros recorridos y el número de litros consumidos, y mostrará un mensaje con el consumo del coche, tanto en litros por cada 100 km como en kilómetros que se pueden recorrer con 1 litro de gasolina..
Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off.
baypath15: 67%, baypath20: 37%, baypath12: 67%, baypath18: 73%, baypath11: 63%, baypath14: 53%, baypath17: 53%, baypath13: 40%
Write a short program that will assign 71 to a variable that represents current temperature, and then increment its value first by one, and then by two. Display the value of the temperature variable (should be 74), and its type.
For the three expressions below in the box (line 5, line 7 and line 9), write their shorthand notations.
Q-1: Match each component of the expression a = square(3) + 1 with the right box on the right.
• a
• variable (name)
• =
• assignment
• square
• function name
• square(3)
• function call
• square(3) + 1
• 10

Initialize two variables to values 9 and 2 respectively. Then:

1. increment each variable first by value 2 (means you update those variables)

2. integer divide the two variables (divide the larger value with the lower one), and assign the result to another variable called c

3. print the variable c and make sure you have the desired result

Q-1: Comments in Python are written with a special character, which one?

Create a variable named carname and assign the value Volvo to it. Then print how many characters carname has.

Warning: you cannot write print(5)

Consider the code below. From line 1-5 you have a function called even that checks whether a number is even or not. If it is even, the function returns true, otherwise it returns false. In line 7 there is a variable called a and is initialized to value 4. Modify the code below so to:

1. Call function even to check whether a is even or not and print the result to make sure you have it right.

2. Increment a by one and call the function again and print the result again.

Write a program that converts a user’s input of a number of hours (for example, 5) into minutes (for example, 300).
Write a program that asks a user for an input sentence (for example, I love Python!) and prints the number of characters in the sentence (for example, 14).
Q-1: Assume that variable a is initialized to 3 with this line of code: a = 3. Match each piece of code on the left, to the right final value of a on the right if you execute each of the expressions on the left independently.
• a = a + 1
• 4
• a = a
• 3
• a -= 2
• 1
• b = a + 1
• 3
• a = square(a)
• 9
• b = square(a)
• 3

Q-1: Evaluate the following numerical expressions in your head, then use the active code window to check your results:

1. 5 ** 2

2. 9 * 5

3. 15 / 12

4. 12 / 15

5. 15 // 12

6. 12 // 15

7. 5 % 2

8. 9 % 5

9. 15 % 12

10. 12 % 15

11. 6 % 6

12. 0 % 7

Evaluate the following numerical expressions in your head, then use the active code window to check your results:

1. 5 ** 2

2. 9 * 5

Add parentheses to the expression 6 * 1 - 2 to change its value from 4 to -6.

Optional. Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle and store the values in variables called width and height. Print a nice message with the answer.

Challenge: Write a program that will convert degrees celsius to degrees fahrenheit.

Ask the user for the temperature in Fahrenheit and store it in a variable call deg_f. Calculate the equivalent temperature in degrees Celsius and store it in deg_c. Output a message to the user giving the temperature in Celsius.

Ask the user for the temperature in Fahrenheit and store it in a variable call deg_f. Calculate the equivalent temperature in degrees Celsius and store it in deg_c. Output a message to the user giving the temperature in Celsius.

Below the code already provided for you, increment i by 10, decrement j by 200, use the *= operator to increase k by a factor of 1.5 and reassign l to 42.

Write a program to print out the root mean squared speed of hydrogen molecules in a gas where

$v_\text{rms} = \sqrt{\frac{3k_\text{B}T}{m_0}}$

where $$k_\text{B}=1.381\times 10^{-23}~\text{J/K}$$ is Boltzmann’s constant, $$m_0=2(1.67\times10^{-27}~\text{kg})$$ and $$T$$ is the temperature supplied by the user. Please use the variable temperature to hold the user supplied temperature as a float.

The wavelengths of the spectral lines from hydrogen are given by the Balmer series

$\frac{1}{\lambda} = R_\text{H}\left(\frac{1}{2^2} - \frac{1}{n^2}\right)\quad n=3,4,5,\dots$

where $$\lambda$$ is the wavelength in m and $$R_\text{H}=1.0974\times10^7~\text{m^{-1}}$$. Print out the wavelengths of the first five spectral lines.

Write a program that will compute the area of a circle. Prompt the user to enter the radius and save it to a variable called radius. Use 3.14 for the value of $$\pi$$. Print a nice message back to the user with the answer.

Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Store the values as type float in variables call miles and gallons respectively. Print a nice message with the answer.

Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?

Write in Python the following expression: 2^3 (two raised to the power of 3) and assign it to a variable called result. Print the value of variable result. You should see 8 as your result.

Q-1: Consider the following Python command: print("Hello, World"). What does print represent here?

• a function
• Yes, this represents function call. print function takes an argument that it prints on your screen.
• a value
• Value is something you cannot call like: "Hello, World", 3, 1.5, and so on.

Q-1: Variable names can contain spaces?

• True
• No, variable name cannot use spaces, use _ instead.
• False
• Correct.

Q-1: Is me+you a valid variable name?

• True
• No, variable name cannot use special character unless it's an _.
• False
• Correct.

Correct the code below that converts minutes into seconds. When a user provides a number of minutes, it outputs the minutes converted to seconds. E.g., when a user provides number 1, the output should be 60. Work until you get the right answer!

Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on a 24 hour clock when the alarm goes off.

If this is a homework problem instead of an example in the text then the assignment text should go here. The assignment text ends with the line containing four tilde

Create a variable named carname and assign the value Honda to it. Then print how many characters carname has.

Warning: you cannot write print(5)

Write code to determine how many times “West Virginia” appears in the lyrics to Country Roads. Store the number in a variable named country_roads_times.

Q-1: Suppose Python gives you this error: TypeError: cannot concatenate 'str' and 'int' objects on line 1

What does this error mean? How would you fix it?

Evaluate the following numerical expressions in your head, then use the active code window to check your results:

1. 6 ** 2

2. 9 * 5

3. 15 / 12

4. 12 / 15

5. 15 // 12

6. 12 // 15

7. 5 % 2

8. 9 % 5

9. 15 % 12

10. 12 % 15

11. 6 % 6

12. 0 % 7

Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on a 24 hour clock when the alarm goes off.

Evaluate the following numerical expressions in your head, then use the active code window to check your results:

1. 6 ** 2

2. 9 * 5

3. 15 / 12

4. 12 / 15

5. 15 // 12

6. 12 // 15

7. 5 % 2

8. 9 % 5

9. 15 % 12

10. 12 % 15

11. 6 % 6

12. 0 % 7

Write code that asks users to enter the year they were born. Print out how many years old they will turn in 2019.

Write a program that asks for the user’s name and age (in years), then tells the user how old they are in days. Don’t worry about leap year.

Write a program that will compute the area of a trapezoid. Prompt the user to enter base 1, base 2 and height and save them to variables called a, b and h. Print a nice message back to the user with the answer.

The degree Fahrenheit (symbol: °F) is widely used in the USA. However, Noah’s Dad is more used to the degree Celsius (symbol: °C). Help Noah’s Dad to write a program to convert the degree Fahrenheit to the degree Celsius. You can do some research for the conversion formula. Your code should: 1. Ask the user for the temperature in Fahrenheit and store it in a variable called fahrenheit. 2. Calculate the equivalent temperature in degrees Celsius and store it in a variable called celsius. 3. Output a message to the user giving the temperature in Celsius.

Write a program to accept an integer num as the input and output a boolean value to determine whether it is an odd number or not.

Write a program to accept two integers a and b as the input. Output the lcm (least common multiple) of them. This program will not be auto graded (You tutor will grade it manually). To help you to verify the correctness of your program, here are some test cases you can use: lcm(16, 24) = 48, lcm(8, 12) = 24, lcm(6, 9) = 18, lcm(20, 28) = 140, lcm(18, 81) = 162

Write a program that will convert tablespoons to teaspoons. This program will also need to get input from a user to see how many tablespoons should be converted and the result should be printed to the user.

Write a program that will convert tablespoons to teaspoons. This program will also need to get input from a user to see how many tablespoons should be converted and the result should be printed to the user.

Q-1: Write a short essay on the meaning of life.

Correct the code below to output number 5.

Consider the initialized variables in the code below I created for you. Add code after line 7 to check and print their types (don’t touch/change lines 1 - 7).

Q-1: What value is printed in the final line of code below?

glass_of_water = 3
glass_of_water = glass_of_water + 1
print(glass_of_water)

• 4
• Correct. You first assign value 3 to the variable, and then you reasign it the old value + 1.
• 3
• Not quite. The value of glass_of_water changes.
• 1
• Not quite. The value of glass_of_water is initially 3, then you take its value (3) and add 1 to it and assign it back to glass_of_water.
• glass_of_water
• Hmm no. glass_of_water is a variable, and when we print variables we actually print their values. Compare to: print("glass_of_water")

Q-1: What value is printed in the final line of code below?

people = 1
people = people + 1
people = people + 1
people = people + 1
people = people + 1
print(people)

• 1
• Not quite. Review 2.12 Reassignment section.
• 7
• Not quite. Review 2.12 Reassignment section.
• 5
• Correct. People variable is initially one, and its valued is increased by one 4 times.
• people
• Hmm no. people is a variable, and when we print variables we actually print their values. Compare to: print("people")

Write a calculator or tool for another class. For example (you therefore can’t use this) for a history class you might make a calculator that is given a historical date and returns how many years since that event transpired. You may add a list of historical events that the user can select, that returns the years since those events. Your calculator should get user input, and the output should vary based on the users input. Demonstrate the use of conditionals. Points for creativity as well. Your calculator should have error messages if the input is invalid (i.e. if ‘’January’’ is given instead of a year.)

(2 points) Ada Lovelace and Charles Babbage are trapped in a dimensional warp and need your help to escape before they are eaten by a grue. They need you to write a program that will calculate the dimensional escape code they need to exit the warp.

Write a program that prompts the user to input their three dimensional coordinates (integers), storing them in the variables z, q, and d.

To compute the dimensional escape code, multiply z by 352, divide q by 17, raise d to the 7th power. Add the result of all three of these operations together and store the result in a variable called escapeCode. Note that the result of this operation could be a floating-point number.

Do not alter what is stored in z, q, or d during these operations, as this could break the space-time continuum. (It will also break my tests if you change them.)

Once you have completed this program and it passes our checks, our system will transmit and translate the instructions to the doomed pair. (In other words, I have included a runtime test so that you can check your work.) Click the Show Code button to begin.

Challenge: Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle and store the values in variables called width and height. Print a nice message with the answer.

It is possible to name the days 0 thru 6 where day 0 is Sunday and day 6 is Saturday. If you go on a wonderful holiday leaving on day number 3 (a Wednesday) and you return home after 10 nights you would return home on a Saturday (day 6).

Write a general version of the program which (1) prompts the user for a starting day number and the length of their stay, then (2) stores the values as type int variables start_day and stay_length respectively, and finally (3) prints the number of the day of the week they will return in an appropriate message.

Ask the user for the temperature in Fahrenheit and store it in a variable call deg_f. Calculate the equivalent temperature in degrees Celsius and store it in deg_c. Output a message to the user giving the temperature in Celsius.

The formula for computing the final amount if one is earning compound interest is given on Wikipedia as

$A = P\left(1+\frac{r}{n}\right)^{nt}.$

Write a Python program that assigns the principal amount of 10000 to variable $$P$$, assign to $$n$$ the value 12, and assign to $$r$$ the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, $$t$$, that the money will be compounded for. Print a message to the user giving the final amount of money after $$t$$ years.

Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Your program should first prompt the user for the time now (in hours) and for the number of hours to wait for the alarm, then store those values as type int variables time_now and wait_time respectively, and finally print the time on the 24 hour clock when the alarm will go off.

Ada Lovelace and Charles Babbage are trapped in a dimensional warp and need your help to escape before they are eaten by a grue. They need you to write a program that will calculate the dimensional escape code they need to exit the warp.

Write a program that asks the user to input their three dimensional coordinates, storing them in the variables z, q, and d, storing each as a number, not as a string. To compute the dimensional escape code, multiply z by 352, divide q by 17, raise d to the 7th power. Do not alter what is stored in z, q, or d during these operations, as this could break the space-time contin… space-time continueinueiueum… look, I’m new to this. It will break our tests if you change them. Add the result of all three numbers of these operations together and store the result in a variable called escapeCode.

Once you have completed this program and it passes our checks, our system will transmit and translate the instructions.

Arrange the code so that a will store the value 30 and b will store the value 10

Write code that asks the user the year of his/hers birthday. Then the code prints “You are #answer many years old”.

Write a program that will convert gallons to liters. This program will also need to get input from a user to see how many gallons should be converted and the result should be printed to the user.

Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Store the values as type float in variables called miles and gallons respectively. Print a nice message with the answer.

The formula for computing the final amount if one is earning compound interest is given on Wikipedia as

$A = P\left(1+\frac{r}{n}\right)^{nt}.$

Write a Python program that assigns the principal amount of 10000 to variable $$P$$, assign to $$n$$ the value 12, and assign to $$r$$ the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, $$t$$, that the money will be compounded for. Calculate and print the final amount after $$t$$ years.

Write a program that will compute MPG (“miles per gallon”) for a car.

Prompt the user to input the number of miles driven and store it in a variable called miles and the number of gallons used stored in a variable gallons. Print a nice output message with the answer. (Hint: this will require two different input statements in your program.)

Remember: please include your name as a first line comment. The auto-tests are only checking some of your program; you will need to determine yourself whether or not your program meets all of the specifications above.

Write a Python program that asks the user to enter the temperature in degrees Fahrenheit and stores it in a variable called deg_f. Then, calculate the equivalent temperature in degrees Celsius and store it in a variable called deg_c. Finally, output a message to the user giving the equivalent temperature in degrees Celsius.

You may need to use the Internet to find the conversion formula. Remember: input from the keyboard is treated as a string; so, if you want to interpret it as a number, you’ll need to call the appropriate conversion function. For this exercise, assume we want to use numbers with decimal points, i.e. floats.

Write a program that does the following:

1. asks the user for their favorite positive integer,

2. stores the number provided by the user in a string variable called number_str,

3. prints a message telling the user how many digits are in that number,

4. stores the value of the number in an int variable called number_int, and then

5. prints a message telling the user the value of their number after it is cubed.

For example, if the user enters the integer 2, the program should tell them there is 1 digit in that number, and that the cube of 2 is 8. If the user enters 10, the program should tell them there are 2 digits in that number, and that the cube of 10 is 1000.

Write a program that will compute the area of a circle. Prompt the user to enter the radius and save it to avariable called radius. Print a nice message back to the user with the answer.

Write code that asks users to enter the year they were born. Print out how many years old they will turn in 2021.

Write code that asks users to enter the year they were born. Print out how many years old they will turn in 2025.

Q-1: In your own words, describe how classes are used in python. Use examples

config values (conf.py):

• shortanswer_div_class - custom CSS class of the component’s outermost div