I\'m trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I\'m using python 3.
def collatz(number):
if number % 2 == 0: # Even number
return number // 2
elif number % 2 == 1: # Odd number
return number * 3 + 1
print('Please enter a number') # Ask for the number
# Check if the number is an integer, if so, see if even or odd. If not, rebuke and exit
try:
number = int(input())
while number != 1:
collatz(number)
print(number)
number = collatz(number)
else:
print('You Win. The number is now 1!')
except ValueError:
print('Please enter an integer')
This is what I came up with for this practice exercise. It asks for an input Validates whether it's an integer. If not it rebukes and exits. If it is, it loops through the collatz sequence until the result is 1 and then you win.
Here's my 19 lines:
def collatz(number):
if number % 2 == 0:
return number // 2
else:
return number*3 + 1
number = 0
while number == 0:
try:
number = int(input('Please enter a number: '))
if number == 0:
print('Number must be an integer not equal to zero.')
else:
while True:
number = collatz(number)
print(number)
if abs(number) == 1 or number == -5 or number == -17:
break #Collatz seq ends/enters recurring loop when number hits -17, -5, -1 or 1
except ValueError:
print('Number must be an integer.')
Here's what I came up with:
import sys
def collatz(number):
if number % 2 == 0: # Even number
result = number // 2
elif number % 2 == 1: # Odd number
result = 3 * number + 1
while result == 1: # It would not print the number 1 without this loop
print(result)
sys.exit() # So 1 is not printed forever.
while result != 1: # Goes through this loop until the condition in the previous one is True.
print(result)
number = result # This makes it so collatz() is called with the number it has previously evaluated down to.
return collatz(number)
print('Enter a number: ') # Program starts here!
try:
number = int(input()) # ERROR! if a text string or float is input.
collatz(number)
except ValueError:
print('You must enter an integer type.')
# Fully working!
14 lines:
Don't get why we need "elif number %2 == 1:" instead of simple 'else'?
def collatz(number):
while number != 1:
if number %2 == 0:
number = number/2
print(number)
else:
number = 3*number+1
print(number)
print('Enter a number')
try:
number = (int(input()))
except ValueError:
print("Please enter an INTEGER.")
collatz(number)
def collatz(number):
if number % 2 == 0:
print(number//2)
return number // 2
elif number % 2 == 1:
print(3*+number+1)
return 3 * number + 1
r=''
print('Enter the number')
while r != int:
try:
r=input()
while r != 1:
r=collatz(int(r))
break
except ValueError:
print ('Please enter an integer')
I added input validation
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1
try:
chosenInt = int(input('Enter an integer greater than 1: '))
while chosenInt < 2:
print("Sorry, your number must be greater than 1.")
chosenInt = int(input('Enter an integer greater than 1: '))
print(chosenInt)
while chosenInt != 1:
chosenInt = collatz(chosenInt)
print(chosenInt)
except ValueError:
print('Sorry, you must enter an integer.')