Where is my error?

num = str(input('Enter a number: '))
print (num)

def Collatz(num):
  if num % 1 == 0 and num % 2 == 0:
    return num / 2
  elif num % 1 == 0 and num % 2 != 0:
    return (3 * num) + 1
print (Collatz(num))

“”“when I run, I can enter a number, but it then says:
'Traceback (most recent call last):
File “python”, line 10, in
File “python”, line 5, in Collatz
TypeError: not all arguments converted during string formatting’
Can someone please tell me what I am doing wrong”""


do you run python3?

why would you convert the input by input() to string if you are working with numbers?


not gonna lie I don’t know I only just started using python today also I’m using python 3.6.1



num = str(input('Enter a number: '))

you convert to string using str(), why?

working with numbers, strings is not the right data type


not sure I did that with a different code so tried it again because I assumed it would work, don’t worry though I’ve resolved it, changed str() to float(), but thanks anyway!


quick question though, how would I repeat this function until the value of the function reached 1?


anything that need repeat would require a loop


And how do you do that?


well, you would first need to decide what kind of loop you would use, while or for

once you decide what loop you are going for, you need to check the value of num in the loop condition.

in the loop, you need to do something to ensure num becomes 1 in the end.


How do I decide which one I want to use? sorry am a beginner lol


you would need to understand how both loop works (the basics), then decide which best suits your situation.

in your case, i would very likely go with a while loop.


Ok thank you, I will look into how to use those


Thanks loads for your help haha


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.