1

My first question on this forum and im completely new to programming, so apologies if i do something wrong.

So im trying to program something to do a collatz sequense on a number i put in. To do that i have to check if the number i put in is even or odd. The easy fix would be to use the built in % 2 function but i want to do it "dirty" to learn better.

So my plan was to check if the last number in said input number is in the list (0, 2, 4, 6, 8) since an even number ends on those and is odd otherwise. Problem is how to check the very last index in the number to see if its in that list. I tried the following code

def Collatz(input_number):
    input_number_num = int(input_number)
    lenght = len(input_number_num)
    position = lenght - 1
    if [position] in input_number_num is in (0, 2, 4, 6, 8)
        return (input_number_num / 2)
    else:
        return (input_number_num * 3 + 1)

This gives me a syntax error, im guessing since it reads [lenght] as an tuple rather than the index.

Daniel .S
  • 35
  • 4

1 Answers1

0

You could convert the number to a string and then use the -1 index to extract the last character:

if input_number[-1] in ('0', '2', '4', '6', '8'):

But to be completely honest, I can't see any advantage of doing this over just using % 2.

Mureinik
  • 297,002
  • 52
  • 306
  • 350