Skip to content

Review Questions

Review Questions

For Loops

Q08.01 Create a for loops to print out the numbers 1 to 10.

Q08.02 Create a for loop to print out the number -1 to -10 starting at -1 and ending at -10.

Q08.03 Create a for loop to print out all the letters in the word 'love'

Q08.04 Use a for loop to sum the elements in the list [1,3,5,8,12]. Print the sum to the user.

Q08.05 Create the Fibonacci sequence using a for loop. Print out the first 20 terms of the Fibonacci sequence on one line.

Q08.06 (a) Use a for loop to print out the numbers 1 to 30

(b) Use a for loop to print out all the numbers 1 to 30, but leave out any number which is divisible by 3, such as 3,6 and 9.

(c) Use a for loop to print out all the numbers 1 to 30, but leave out any number which is divisible by 5, such as 5,10 and 15.

(d) Use a for loop to print out all the numbers 1 to 30, but insert the word fizz for any number that is divisible by 3, insert the word buzz for any number that is divisible by 5 and insert the word fizz buzz for any numbers that are both divisible by 3 and 5, like 15.

Q08.07 Imagine you can see the future of investing and over the next four years, the interest rate of return on investments is going to be 0.02, 0.03, 0.015, 0.06. Prompt the user for an initial investment with Python's input(_) function and use the formula below to calculate how much the investment will be worth after four years.

new balance = old balance + old balance \times interest rate

Note the first "old balance" is the person's initial investment.

Q08.08 A geometric series is a series that has a common ratio between the terms. The sum of the geometric series that starts at \frac{1}{2} and has a common ratio of \frac{1}{2} approaches the value 1.

The formula that shows the sum of a geometric series which approaches 1 is below.

1 = \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{16} + ...

Use the geometric series above to approximate the value of 1 after 10 terms are added. Print out how far off the geometric series approximation is to 1.

Q08.09 A Taylor Series is an infinite series of mathematical terms that when summed together approximate a mathematical function. A Taylor Series can be used to approximate e^x, sine, and cosine.

Taylor Series expansion for the function e^x is below:

{e^x} = \sum\limits_{n = 0}^\infty {\frac{{{x^n}}}{{n!}}} = 1 + x + \frac{{{x^2}}}{{2!}} + \frac{{{x^3}}}{{3!}} + \frac{{{x^4}}}{{4!}} + ...

Write a program that asks a user for a number x, then calculates e^x using the Taylor Series expansion. Calculate 20 terms.

While Loops

Q08.40 Use a while loop to sum the elements in the list [1,3,5,8,12]. Print the sum to the user.

Q08.41 Use a while loop to print out the numbers between 1 and 100 that have whole number square roots.

Q08.42 Create a program that prompts a user for test scores. Continue to prompt the user for additional test scores until the user types 'q'. When the user types 'q', the program stops asking the user for test scores and prints out the following statistics about the test scores the user entered:

  • mean
  • median
  • standard deviation

Q08.43 Use while loop to validate user input. Ask the user to enter a value between 0 and 1. Print back to the user "Try again" if the user's input is invalid. Print "Your number works!" when the user enters valid input.

Q08.44 Use a while loop to validate user input. Ask the user to enter a day of the week. Keep asking the user for a day of the week until they enter one. When the user enters a day of the week, print back to the "Yup, it's <day of the week>".

Q08.45 Write a program to play the game higher/lower. Tell the user you have picked a random integer between 1 and 20.

The code below creates a random integer n between 1 and 20:

from random import randint
n = (randint(1, 20))

(a) Ask the user to enter a number (one time) and tell the user if the number random is higher or lower. Print higher if the random number is higher than the user's guess, print lower if the random number is lower than the user's guess. Print You guessed it: <random number> if the user guesses the random number.

(b) Modify your program so that the program keeps printing higher or lower after each guess until the user guesses the random number. When the user guesses the random number print You guessed it: <random number>.

(c) Extend your higher/lower game to record the number of guesses the user enters to guess the random number. Then the user guesses the random number print You guessed: <random number in <number of tries>.

Q08.46 A Taylor Series is an infinite series of mathematical terms that when summed together approximate a mathematical function. A Taylor Series can be used to approximate e^x, sine, and cosine.

Taylor Series expansion for the function e^x is below:

{e^x} = \sum\limits_{n = 0}^\infty {\frac{{{x^n}}}{{n!}}} = 1 + x + \frac{{{x^2}}}{{2!}} + \frac{{{x^3}}}{{3!}} + \frac{{{x^4}}}{{4!}} + ...

Write a program that asks a user for a number x, then calculates e^x using the Taylor Series expansion. Continue to add terms to the Taylor Series until the result from the Taylor series is less than 0.001 off the value of e^x calculated with Python's math.exp() function.

Errors, Explanations, and Solutions

Run the following code snippets. Explain the error in your own words. Rewrite the code snippet to solve the error.

Q08.80

n = [1 2 3]
for n[1] == 2:
    n = n + 1
end

Q08.81

while x in [1, 2, 3]:
    print(x)

Q08.82

n = 1
while 1 == n
    print('valid')
    n = n +1

Q08.83

for i in range(3):
print(i)