Skip to content
Advertisement

How can I create a loop with my conditions

i am looking for help. We need to write a program that prints all numbers in the range of (n -20,n + 20). In addition, the program asks you beforehand to input a number. If that number is not even or multiple of 10, you need to take a guess again. Only if the number is even and multiple by 10 the program prints the range aforementioned. I struggle with that.

I came up with that solution:

    i = int(input("please enter a number: "))
    while (i % 10 == 0) and ((i % 2) == 0):
        x = 20
        while (x >= 0):
            print(i - x)
            x = x - 1
        break

but it will only print the range n-20 and not +20 and it also won’t ask you again if you input a false number.

I know there is also the possibility to use for I in range() but I am at a loss for ideas at the moment.

Thank you!

Advertisement

Answer

You can simply do:

while True:
  i = int(input("please enter a number: "))
  if i % 10 == 0:
       for x in range(i-20,i+21):
           print(x)
       break

It will keep on asking until it satisfies the condition.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement