Implementing the collatz function using Python

2.3k views Asked by At

I am currently having trouble completing this challenge in "Automate the boring stuff":

Image of the challenge

My code is:

def collatz(number):
    global seqNum
    if (seqNum % 2 == 0):
        return seqNum // 2
    elif (seqNum % 2 == 1):
        return 3 * seqNum + 1


print('What number would you like to use?')
seqNum = input()
number = int(seqNum)
i = number

while i > 1:
    collatz(seqNum)
    print(number)

And I am getting this error:

"Traceback (most recent call last):
  File "C:/Users/Administrative/AppData/Local/Programs/Python/Python36-32/collatzSeq.py", line 15, in <module>
    collatz(seqNum)
  File "C:/Users/Administrative/AppData/Local/Programs/Python/Python36-32/collatzSeq.py", line 3, in collatz
    if (seqNum % 2 == 0):
TypeError: not all arguments converted during string formatting"

I know I am doing SOMETHING wrong with how I wrote my code but I don't understand what it is exactly. Any and all help is greatly appreciated!

Also I am using python 3.

8

There are 8 answers

2
this be Shiva On BEST ANSWER
  1. You're doing arithmetic on a string, not an integer.

  2. There's no need to have a global variable. Pass an argument to a function, and have it return a value accordingly.


def collatz(number):
    if (number % 2 == 0):
        return number // 2

    elif (number % 2 == 1):
        return 3 * number + 1

print('What number would you like to use?')

i = int(input())
while i > 1:     
    i = collatz(i)
    print(i)
3
Bobby Durrett On

seqNum is a string.

>>> "3" % 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
0
Xvolks On

It seems that you should pass i to your function instead of seqNum.

And in your function remove all references to seqNum and use number instead.

0
MSeifert On

There are several problems here, but the one causing your Exception is that you use the seqNum in the function, which is what input() returned. And input() returns a string (at least on Python 3). And for strings the % is the "formatting operator" which also explains the exception message, which talked about "string formatting".

You could write it as follows (using number instead of seqNum):

def collatz(number):  
    # you pass the number to the function and you return, so no need for global        
    if number % 2 == 0:       # python doesn't need parenthesis for "if"s
        return number // 2
    else:                     # it can only be even OR odd so no need to calculate the modulo again
        return 3 * number + 1

# You can put the question as argument for "input" instead of printing it
seqNum = input('What number would you like to use?')  
number = int(seqNum)

while number > 1 :
    number = collatz(number)   # assign the result of the function to "number"
    print(number)
1
Bayo On
def collatz(number):
    if number % 2 == 0:
        return number // 2
    else:
        return 3 * number + 1


try:
    print("Enter a number: ")
    i = int(input())
    while i > 1:
        i = collatz(i)
        print(i)
except ValueError:
    print("You must enter an integer.")
0
Raghav On
# This could be the simplest solution
def collatz(number):
    while True:
        if number <= 1:
            break
        elif number % 2 == 0:
            number = number // 2
            print(number)
        elif number % 2 == 1:
            number = 3 * number + 1
            print (number)
try:
    print('Enter a number \n')
    number = int(input())
    collatz(number)
except ValueError:
    print('Invalid value, Enter a number.')
0
Troy McGregor On

Hi I'm new to coding and also looking at this exercise. If it's helpful here is the approach I took using 1 x function + 2 x while loops. I also notice the program didn't handle a zero value well as an input:

# This program runs the Collatz sequence - Automate book, Chapter 3 practice project
# It includes the 'Input Validaton' additional exercise
# It also inlcudes a further test for zero value input as this makes collatz non-terminating

def collatz(number):
    #test even
    if number % 2 == 0:
        return number // 2
    #or implicit it is odd
    else:
        return 3 * number + 1

# Get the user input and validate - loop continues until non-zero integer entered
while True:
    try:
        print('Enter a non-zero number')
        number = int(input())
        if number == 0:
            continue
        else:
            break
    except ValueError:
        print('Error: You must enter and integer')

# Iterate over the input number until it == 1              
while number != 1:
    # return value assigned to global var
    number = collatz(number) 
    # output the result of collatz to screen
    print(number)
0
F_DiGi On

I think this is simplest to understand for a beginner like me:

def collatz(number):
while number != 1:
    if number % 2 == 0:
        number = number // 2
        print(number)
    else:
        number = 3 * number + 1
        print(number)

try:
    collatz(number=int(input("Enter the number: \n")))
except ValueError:
    print('Error: Invalid argument \nPlease enter a number')