4.10. Exercises: Data & Variables¶
Exercises appear regularly in the book. Just like the concept checks, the exercises check your understanding of the topics in this chapter. They also provide good practice for the new skills.
4.10.1. Part A - Printing to the Console¶
Use the code editor at the end of this part to complete the following tasks:
Add parenthesis to the expression
6 * 1 - 2to change its value from
2 + 8 + 10 / 5evaluates to
12.0. Add parentheses to change the result to
4.0. How could you get a result of
Assign a string of your choice to a variable named
Print the word 4 times on the same line, with spaces between each -
word word word word.
Using a single
wordword wordword wordword
Using any number of
word word word word word word word word word
Note that each
wordis separated by a tab, not a space.
4.10.2. Part B - Using Variables¶
The remaining exercises require more space than the small embedded code editor. You will need to move to a larger workspace.
If your teacher uses a Trinket course, follow the links for instructions on how to access this assignment.
Use the information given below to complete the part B exercises:
|Name of the spaceship||Determination|
|Ship Speed (mph)||17,500|
|Distance to Mars (km)||225,000,000|
|Miles per kilometer||0.621|
- Create a variable for each value in the list above. Remember, do NOT include
- Create and assign a variable to hold the miles to Mars value. To get the miles to Mars,
multiply the distance to Mars in kilometers by the miles per kilometer.
(Let Python to the math!
distance in km * miles per km)
- Create a variable to hold the hours it would take to get to Mars. To get the hours, you need to divide the miles to Mars by the spaceship’s speed.
- Create a variable to store the value for the days to Mars. To get the days it will take to reach Mars, divide the hours it takes to reach Mars by 24.
- Using your variables, print to the console the sentence,
"_____ will take ___ days to reach Mars."Fill in the blanks with the spaceship name and the calculated time.
- Bonus: The distance to the moon is 384,400 km. Repeat the calculations,
but this time figure out the number of days it would take to travel to the
Moon. Print a sentence that says,
"_____ will take ___ days to reach the Moon.".
4.10.3. Part C - User Input¶
len() function returns the number of characters in a string.
1 2 3 4
a_string = 'Rutabaga' print(len('the')) print(len(a_string))
Prompt the user to enter a word, then use the
len()function to find the number of characters in the word. Print the message,
The word '___' contains ___ characters.Fill in the blanks with the user’s word and the number of characters. The output MUST include quotes around the word. For example:
Enter a word: Tomato The word 'Tomato' contains 6 characters.
Prompt the user to enter the length and width for a rectangle. Calculate the area of the rectangle (
length * width) and print the answer. The program should behave something like this:
Rectangle length: 8 Rectangle width: 4 The rectangle has an area of 32.
Write a program that will find the miles per gallon for a car. Prompt the user to enter the number of miles driven and the number of gallons used. The program should behave something like this:
How many miles did you drive? 280 How many gallons did you use? 10 Your car got 28 miles per gallon.