5

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.

cs95
  • 379,657
  • 97
  • 704
  • 746
Xert01998
  • 99
  • 1
  • 5
  • You aren't using your argument... I think you want to use number instead of seqNum. And this doesn't work because input returns a string, which is not a number. Also, you don't really need an `elif`, you can just use `else` since the only other possible value is 1. – John Szakmeister Aug 31 '17 at 21:26
  • ^ and get rid of the global declaration there, that isn't helping – FreshPow Aug 31 '17 at 21:29

8 Answers8

4
  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)
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Also I had a question about this: So you wrote "while True" since I'm calling the collatz() function and the function has no possible values of False? Also can I use whatever expression after while? So if I wanted I could write while False, 1 < 2, etc. ? Sorry for the dumb questions just trying to help myself understand this – Xert01998 Aug 31 '17 at 23:15
  • I ran it with 12 and received the full sequence? am confuse – Xert01998 Aug 31 '17 at 23:22
3

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)
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1

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
>>>
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
Bobby Durrett
  • 1,223
  • 12
  • 19
1
# 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.')
Raghav
  • 31
  • 2
1

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')
F_DiGi
  • 11
  • 1
0

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.

Xvolks
  • 2,065
  • 1
  • 21
  • 32
0

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
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.")
Bayo
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 04 '22 at 03:44