Cleaning the user input validation
- Remove
continue
because in both cases the loop will continue anyway, only break
will stop it, so do not worry.
- Do not make the user think:
number = int(input("Enter number:"))
: which number to enter? Sure you know, but messages are intended to communicate with the user, so do not make him guess what you want, but be clear and precise with him.
Cleaning collatz()
- Please apply the naming conventions and indentation rules.
- Leave space between operators and their operands (example:
newNumber=3*number+1
should be written newNumber = 3 * number + 1
)
- Do not borrow
;
sysntax into Python. You should remove it.
- Remove unnecessary comments: when you code
(number %2 ==0 ) :#even
, I think everybody is smart enough to guess you are processing the case where number
is even. The same thing goes for #odd
and #Input validation loop
.
- Remove
return newNumber
, it does not make sense because return (newNumber)
is enough to exit the recursion when newNumber == 1
.
So under the shadow of what is mentioned above, let us clean collatz()
:
def collatz(number):
if (number %2 == 0 ):
new_number = number // 2
print(new_number);
if new_number!=1:
collatz(new_number);
else:
return new_number
elif (number % 2 == 1):
new_number = 3 * number + 1
print (new_number)
collatz(new_number)
Improving the input validation
- What you have done so far is good, I suggest you to handle the case where the user presses Ctrl + c to exit your program, because when I tried to do so, I got an ugly
KeyboardInterrupt
exception.
- Think of code reuse, I mean the input validation could be re-structured as a function, this way you can use it elsewhere easily if similar purposes are encountered.
Given these last 2 elements, let us re-write the code validation:
def validate_user_input():
while True:
try:
number = int(input("Enter a positive integer number ( > 1): "))
except KeyboardInterrupt:
print('\nSee you next time!')
break
except ValueError:
print("That was not even a valid integer!")
else:
if number < 0:
print("Positive integer, please!")
elif number < 2:
print("Integer should be at least equal to 2 !")
else:
return number
Improving collatz()
You said you do not want to see other solutions and you want only to see if you can improve your own one. So let us see if we can do so.
From your solution we can see:
- You always call
collatz()
whether new_number
is odd or even.
- You always print
new_number
whether it is odd or even
- The only useful return statement is the once which corresponds to
new_number = 1
.
Let us translate the 3 phrases above into code:
callatz(new_number)
print(new_number)
return new_number
We should not forget the shared fact: new_number can be either odd or even (so we do not care about that)
new_number = n//2 if n % 2 == 0 else n*3 + 1
Now we are ready to gather the 4 instructions listed above into one following their coherent flow:
def collatz(n):
if n == 1:
return
else:
new_number = n // 2 if n % 2 == 0 else n * 3 + 1
print(new_number)
collatz(new_number)
Putting all together
Let us gather the code written so far to create an MCVE:
def validate_user_input():
while True:
try:
number = int(input("Enter a positive integer (minimum 2): "))
except KeyboardInterrupt:
print('\nSee you next time!')
break
except ValueError:
print("That was not even a valid integer!")
else:
if number < 0:
print("Positive integer, please!")
elif number < 2:
print("Integer should be at least equal to 2 !")
else:
return number
def collatz(n):
if n == 1:
return
else:
new_number = n//2 if n % 2 == 0 else n*3 + 1
print(new_number)
collatz(new_number)
if __name__ == '__main__':
print('Collatz Conjecture Collatz Conjecture')
number = validate_user_input()
collatz(number)
P.S. Just in case: you can read about if __name__ == "__main__":