Dec-16-2017, 06:56 AM
First, when you post some code, put in between
So, let's have a look:
0. We input the value 2
1. We execute is_hot(2)
2. As N is not strictly less than 2, we branch to the elseif
3. We evaluate is_hot(N/2) --> is_hot(1)
4. We execute is_hot(1)
5. This time, as N is strictly less than 2, we return "cold"
6. So, the "is_hot(N/2)" we are evaluating at step 3 has the value of "cold"
7. We evaluate "cold" > 1 --> BINGO!
As I assume this is a homework, I let you find the solution!
[pyt[i][/i]hon]
and [/pyt[i][/i]hon]
tags, so it keeps proper format which is very important in python, as you know!So, let's have a look:
def is_hot (N): if N<2: return ("cold") elif is_hot(N/2) > 1 or is_hot(N-1) > 1: return ("hot") n = float(input(("Enter Number: "))) print(is_hot(n))If you input 1, the program runs OK, good news, but is you input 2, then you have this strange error:
elif is_hot(N/2) > 1 or is_hot(N-1) > 1: TypeError: unorderable types: str() > int()If we go step by step:
0. We input the value 2
1. We execute is_hot(2)
2. As N is not strictly less than 2, we branch to the elseif
3. We evaluate is_hot(N/2) --> is_hot(1)
4. We execute is_hot(1)
5. This time, as N is strictly less than 2, we return "cold"
6. So, the "is_hot(N/2)" we are evaluating at step 3 has the value of "cold"
7. We evaluate "cold" > 1 --> BINGO!
As I assume this is a homework, I let you find the solution!