Python Online Library of Justice

Asked

Viewed 1,084 times

-1

Read an integer value X. Then display the 6 consecutive odd values from X, one value per line, including X being the case.

Input: the input will be a positive integer value.

Output: the output will be a sequence of six odd numbers.

x=int(input())
impar=0
while(impar<6):
 x=x+1
 impar=+1
 if(x%2 != 0):
   print('%d' %x)

How do I get out of infinite printing and print only 6?

3 answers

4

The main problem in this code:

x=int(input())
impar=0
while(impar<6):
 x=x+1
 impar=+1
 if(x%2 != 0):
   print('%d' %x)

Is that you confused the operator to "add value to variable" - that would be += with =+. How Python ignores spaces between mathematical operators, the sequence impar=+1 simply means impar = +1. That is, its variable that would be the counter is always valid "1", and therefore always less than 6 - and the condition of the while is always true.

On the top line of these, you put in full x = x + 1 - yes, it takes the previous value of x, sum 1, and assigns this new result to the x. The operator += used correctly does just that - so the two lines could be x += 1 and impar += 1 (or x = x + 1 and impar = impar + 1)

That’s not the only mistake in this code - if you fix it, it stops being infinite, and maybe you can fix the rest by analyzing the new results. If you don’t mind, I’ll just wrap up the answer here, and leave some of the challenge back to you. If you still need help, please indicate in the comments.

2

Your program is practically ready, only needing small adjustments that I think you should have done, but in any case "there you go" (with the numbers I tested worked):

x = int(input())
impar = 0
while(impar<11):
 x = x + 1
 impar += 1
 if(x%2 != 0):
  print('%d' %x)

0

This issue relates to the number problem "1070", whose title is "Six Odd Numbers" made available by "Uri Online Judge" (Online Programming Marathon).

See here the entirety of the statement.

In this matter the usuário enter a value inteiro X. Then the program will calculate the NEXT 6 consecutive odd numbers, from the number typed X.

In this matter we must OBSERVE the value of x. If the value of x be odd, he too should be listed on exit. Otherwise should not be listed on exit.

Well, to solve this question we can use the loop of repetition while or for.

To resolve this issue using the while, we can use the following code:

x = int(input())
impar = 0
while impar < 12:
    impar += 1
    if x % 2 != 0:
        print(x)
    x += 1

And, to resolve this issue using the for, we can use the following code:

X = int(input())
for c in range(X, (X + 12)):
    if c % 2 != 0:
        print(c)

Note that in both codes we can notice the value 12 in evidence.

So why use the value 12 in the code?

The value 12 is equal to the product between amount values to be displayed and the footstep range. If the amount of values to be displayed is 6 and the pitch of the range is 2 - because the occurrence of each odd number in the set of integers is 2 in 2 - then, 6 x 2 = 12.

When we execute any of the codes, we receive a screen black with the mouse cursor blinking in the upper left corner of the screen. At this time we must enter an entire number and press enter. From then on, the loop of repetition will perform the possible interactions and, with the help of the block if, will be checked if each temporary value of the loop is odd. If positive, the function print() will display its value.

These codes have already been testados, submetidos and properly aprovados on the website Uri, under programming language Python 3.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.