Problem with program output

My Program Code:

x=10
while x>0:
if x % 2 == 0:
x = x / 2
print(x)

    if x % 2 != 0:
        x = 3 * x + 1
        print(x)

    print(x)
    if x/2==1 or 3*x+1==1:
        break
    print(x)

The Output I am getting:
5.0
16.0
16.0
16.0
8.0
8.0
8.0
4.0
4.0
4.0
2.0
2.0
The Output I Wanted:
5.0
16.0
8.0
4.0
2.0
What am I doing wrong?

Without seeing the problem specification what you’re doing wrong is
unclear.

I would guess that you expect only 1 print() per loop iteration, or
one print() when x is changed.

For the former, a single print at the bottom of the loop might do,
omitting all the others.

For the latter, I suspect you either want some "else:"s to prevent
prints when the “if” are false, or changes to indentation.

For example, you’ve got this:

if x % 2 == 0:
    x = x / 2
print(x)

If you only want to print x when it changes you might change that to
this:

if x % 2 == 0:
    x = x / 2
    print(x)

If you want to know why the extra print()s, give them all distinctive
outputs:

print("a", x)
.......
print("b", x)

Then you can see which prints are unwanted and either comment them out
(to see if things are more correct) or change the logic around them.

Cheers,
Cameron Simpson cs@cskk.id.au

You should achieve the desired result by simply eliminating the third and forth print(x), assuming the first print(x) is at the same indent level as x = x / 2—because you have not used a code block, it is impossible to tell.

However, the logic would be significantly simpler, clearer and less likely to run into these sorts of errors if you only execute one statement per loop iteration, with an if/elif/else block replacing your independent ifs, and only one print statement at the end.

Also, for the rest of us who are not on email like @cameron , please make sure you surround your code with code blocks, as otherwise it is impossible for us to tell what your indent levels are, which is critically important in Python:

```
your code here
```

@zia, are you working on the Collatz conjecture? That is just a guess (a conjecture, as a matter of fact); please provide the problem specification, so that we don’t have to guess what you are trying to do.

Thank you very much for your kind advice. I got the desired result now.

Thanks a lot. I am a novice in python as you might have guessed. Thanks again for your valuable advice.

Thank you very much. I got it working now.