Problem 3 - Loops and math
For this problem, we have to create an algorithm that prints out the squares of all the numbers given a range of two numbers. Remember, we can print each individually, but if the range is large, it's best we have a list instead. We'll also need to iterate in the range, and we have to add 1 to the maximum if we want to include the endpoints.
Take a look at the following algorithm:
ch8_SquareLoops.py
print("This program will print the squares of the numbers in a given range of numbers.") a = int(input("What is the minimum of your range? ")) b = int(input("What is the maximum of your range? ")) Numbers = [] b = b + 1 for i in range(a, b): j = i**2 Numbers.append(j) i = i + 1 print(Numbers)
Notice that we added a j
variable to our algorithm. We didn't use i = i**2
because that would change the value of i
, which would affect our iteration...