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:

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

  2. The expression 2 + 8 + 10 / 5 evaluates to 12.0. Add parentheses to change the result to 4.0. How could you get a result of 5.6?

  3. Assign a string of your choice to a variable named word.

    1. Print the word 4 times on the same line, with spaces between each - word word word word.

    2. Using a single print statement, display this:

      wordword
      wordword
      wordword
      
    3. Using any number of print statements, display this:

      word     word     word
      word     word     word
      word     word     word
      

      Note that each word is separated by a tab, not a space.

Check your solutions.

4.10.2. Part B - Using Variables

Note

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.

If your teacher has NOT set up Trinket course, you can access separate starter code at replit or Trinket. To save and share your code, be sure to login to your account.

Use the information given below to complete the part B exercises:

Data Value
Name of the spaceship Determination
Ship Speed (mph) 17,500
Distance to Mars (km) 225,000,000
Miles per kilometer 0.621
  1. Create a variable for each value in the list above. Remember, do NOT include commas for int or float data types!
  2. 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)
  3. 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.
  4. 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.
  5. 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.
  6. 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.".

Check your solutions.

4.10.3. Part C - User Input

If your teacher uses a Trinket course, solve part C there. Otherwise, use this standard repl or free Trinket.

Note

The len() function returns the number of characters in a string.

1
2
3
4
a_string = 'Rutabaga'

print(len('the'))
print(len(a_string))

Console Output

3
8
  1. 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.
    
  2. 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.
    
  3. 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.
    

Check your solutions.