Hi guys.

I have really general question about this assignment and the thing is I don’t even know which syntax I should know about to find out the solution.

I will appreciate it if you give me some hint about it.

- Do you understand how to check if a number is an Armstrong number? (Understand the exercise.)
- Can you figure out by hand (using pen and paper) if a number is an Armstrong number? (Understand the steps.)
- What steps would you take to do that? (Write pseudocode.)
- Once you have a list of steps, can you convert that to Python?

Yes, I get the idea and I understand it.

I started writing some parts of it. but I don’t know if it is correct or not.

```
step = 0
def steps(number):
if number > 0 :
if number == 1:
return step
elif number % 2 == 0:
return step += 1
else:
number = number*3 + 1
else:
raise ValueError("Only positive integers are allowed")
```

That’s exactly what the purpose of the tests are! Write code, run tests, read the output. If the tests pass, the code is correct. If the tests fail, they should tell you what is wrong with your code.

Do the tests pass? Do they fail? If they fail, would you like help understanding the test output? (That would require you sharing the test output – the CODE RUN and TEST OUTPUT. Please use codeblocks and not screenshots.)

Oh sorry I made a mistake, I meant different excersise, the collatz conjecture.

### Code Run

```
self.assertEqual(steps(16), 4)
```

### Test Failure

AssertionError: None != 4

### Code Run

```
self.assertEqual(steps(12), 9)
```

### Test Failure

AssertionError: None != 9

### Code Run

```
self.assertEqual(steps(1000000), 152)
```

### Test Failure

AssertionError: None != 152

Wrapping the test data in a codeblock would make it easier to read.

- Do you understand what the test is doing?
- Do you understand what the test is expecting and why?
- Do you understand what your code returns and how it differs?
- Do you understand why your code is returning what it returns?

I understand the purpose and logic of the code I wrote. However, I want to handle a situation where a certain condition isn’t met. I’m not sure how to implement this.

```
step = 0
def steps(number): # number = 6
if number > 0 : # True
if number == 1: # False
return step
elif number % 2 == 0: # true
number = number / 2 # 3
if number % 2 == 0: #false
number = number / 2 #skipped
else:
number = number*3 + 1 #10
else:
number = number*3 + 1
else:
raise ValueError("Only positive integers are allowed")
```

If I understand the comments in your code correctly, you try to follow the program flow for a specific example where the argument of the function is `6`

, correct?

```
number = number / 2 # 3
if number % 2 == 0: #false
number = number / 2 #skipped
else:
number = number*3 + 1 #10
```

The comments are correct: When the argument of the function is `6`

, then the last line with a comment assigns the value `10`

to the variable `number`

.

But how do you proceed from there? What does happen next and what does the function return?

Sometimes it helps to describe in English how you, as a human, would approach the task. Imagine you’re competing at the National Collatz Number Championship and the judges want to know how many steps it takes to go from `6`

to `1`

. How would you do it, for this concrete starting value `6`

, without a computer?

well actually this is the part i’m having trouble with.

6/2=3

3%2 != 0 => 3 * 3+1 = 10

10/2=5

5%2 != 0 => 5 * 3 +1 = 16

16/2=8

8/2=4

4/2=2

2/2=1

it would be 8 steps.

[…] it would be 8 steps.

Correct.

Now imagine you want to explain that process to a friend, for any positive number `n`

. That friend is not the brightest candle in the chandelier, so you have to be very specific. How would you do that?

First, I ask them to choose a number. If the number is even (meaning it has a remainder of 0 when divided by 2), they should divide it by 2. If the result is still even, they continue dividing by 2 until they reach 1. If the result is odd, they should multiply the number by 3 and add 1 to it. We continue this process, based on the new result, until we reach 1

If the number is even (meaning it has a remainder of 0 when divided by 2), they should divide it by 2. If the result is still even, they continue dividing by 2 until they reach 1. If the result is odd, they should multiply the number by 3 and add 1 to it. We continue this process, based on the new result, until we reach 1

That sounds about right. But let’s pretend I didn’t understand the last sentence. Can you rephrase it or elaborate on it?

Aaa, that’s a bit difficult :)) but ok.

The whole point of the Collatz Conjecture is that no matter what number you start with, you’ll eventually reach 1 by following these rules

we should keep applying the rules (divide by 2 if even, multiply by 3 and add 1 if odd) to the new result you get after each step.

Is that enough?

How would you translate that last sentence into code? What does “we continue this process” or "“keep applying” look like in Python?

Well i’m not sure, mabey we can write a function and use it several time.

Or using " for " or “while” loop.

Those are both good ideas that will actually work. The first is most commonly called “recursion”, where a function calls itself. The second one uses a loop.

Which one do you want to try first?

I think recursion would be a better idea, but I haven’t learnt it yet.

So i guess while loop.

one more thing I wanted to say that is I used conditionals in my code, wouldn’t it work if I continue using it?

I think recursion would be a better idea, but I haven’t learnt it yet.

I’m not sure if recursion is objectively better. *Personally* I would prefer a loop for this problem. But that’s subjective to some degree.

And that choice is not binding. When you’ve solved this exercise with a loop you can go back and solve it again, using recursion. In fact, that’s often useful to get a better understanding.

I used conditionals in my code, wouldn’t it work if I continue using it?

I’m not sure I understand the question.

You will still need conditionals because you have to differentiate between even and odd numbers. The loop or the recursion does not make your conditionals obsolete.

Sorry, now that I look at my question it’s not understandable.

I will work on my code using while loop to see of it works or not.

Thank you. <3

Recursion tends to be (slightly) slower than loops in most cases But don’t let that stop you from learning it! It’s a super powerful tool and sometimes using a loop isn’t a viable option.