Collatz function implementation using Python

I am currently having problems completing this task in Automating Boring Things:

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 get 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 that I don’t know how I wrote my code, but I don’t understand what it is. Any help is appreciated!

I also use python 3.

+5
source share
5 answers
  • You are doing arithmetic for a string, not an integer.

  • There is no need to have a global variable. Pass an argument to the function and return the 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) 
+4
source

There are a few problems here, but the one that throws your exception is that you use seqNum in a function that returns input() . And input() returns a string (at least in Python 3). And for the string % , this is a "formatting operator", which also explains the exception message that said "string formatting".

You can 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) 
+3
source

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 >>> 
+1
source

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

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

0
source

Hi, I am new to coding and also look at this exercise. If this is useful here, this is the approach I used using 1 x function + 2 x while loops. I also noticed that the program did not treat the null value as 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
source

Source: https://habr.com/ru/post/1271403/


All Articles