Home > Mobile >  Assignment to gather upper limits and squared integers
Assignment to gather upper limits and squared integers

Time:12-24

My assignment is:

Please write a program which asks the user to type in an upper limit. The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1. That is, the program prints out powers of two in order.

The execution of the program finishes when the next number to be printed would be greater than the limit set by the user. No numbers greater than the limit should be printed.

Upper limit: 8 1 2 4 8

This is what I've written

limit = int(input("Upper limit:"))
number = 0
power = 0
while power < limit:
    number  = 1
    power = number ** 2
    print(power)

It's almost correct, except it'll print one row too many. For instance, if I input 50, I'll get:

1 4 9 16 25 36 49 64

I know it's because I put while power < limit, but I'm not sure what to do about it.

Edit: Also, I'm supposed to do this without the True conditional.

CodePudding user response:

Hi and welcome to StackOverflow! You could use a break statement, which exits the loop when executed by the program, like below:

while True:
    number  = 1
    power = number ** 2
    if power < limit:
        print(power)
    else:
        break

The loop in this example will continue forever or until the break statement because of the while True: line. This means it will keep executing until the limit is reached. I hope this answers your question!

CodePudding user response:

You can either divide the limit by 2 or just change the starting variables like so:

limit = int(input("Upper limit:"))
number = 1
power = 1
while power < limit:
    print(power)
    number  = 1
    power = number ** 2

CodePudding user response:

You can use log_2(limit) as like:

import math 
limit = int(input("Upper limit:"))
n = int(math.log(limit, 2))   1
for i in range(n):
    print(pow(2,i))

CodePudding user response:

Most simple way:

One of possible ways:

limit = int(input("Upper limit:"))

BASE = 2
i = 0

while BASE ** i <= limit:
  print(BASE ** i)
  i  = 1
Upper limit:8
1
2
4
8

CodePudding user response:

This is the answer to the problem as written. Specifically you asked. "The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1."

First, assign a value of 1 to the number variable number = 1. Then, since each number is a doubling of the previous number you want to assign the multiple of the number variable to number, overwriting it number*=2. The numbers will increase as so: 1,2,4,8,16,32,64,128.

If you want to avoid printing the number that breaks the limit. You need to put print above the calculation. This is so that it always prints the previous value of number and not the next value which at the end, will always be over the limit.

limit = int(input("Upper limit:"))
number = 1
while number <= limit:
    print(number) # print must be before the calculation to ensure it breaks loop without printing the first number above the limit
    number *= 2 # multiply and operator. number *= 2 means number = number*2
  • Related