0

I just completed an assignment, and both scripts function the same ultimatley, but just wanted to know if there were any potential differences, or is it just a matter of style?

if num % 2 ==0 :
             new = num //2
             num = new
             print(num)
             count = count +1
         else:
             new = num *3 +1
             num = new
             print(num)
             count = count +1

or :

if num % 2 ==0 :
             new = num //2
             print(new)
             num = new
             count = count +1
         else:
             new = num *3 +1
             print(new)
             num = new
             count = count +1
]

you can see the new and num are reversed, and the print statement is below instead of above, again both function the same, but I'm learning python, and just want to make sure I'm learning properly and what seems like a matter of coding style can lead to disfunction down the road, or rather I dont want it to lead to disfunction!

Edited by as3g: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Gribouillis
0

There is no difference. The variable 'new' serves no purpose. The recommended style is

num = 3 * num + 1 if num % 2 else num // 2
print(num)
count += 1

The other highly recommended thing is to indent python code with 4 spaces. Your editor can be configured to insert 4 spaces when you hit the tab key. Read pep 8 for more advices about coding style http://www.python.org/dev/peps/pep-0008/ .

Edited by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.