Where is my error?


#1
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”""


#2

do you run python3?

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


#3

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


#4

here:

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

you convert to string using str(), why?

working with numbers, strings is not the right data type


#5

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!


#6

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


#7

anything that need repeat would require a loop


#8

And how do you do that?


#9

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.


#10

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


#11

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.


#12

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


#13

Thanks loads for your help haha


#15

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