0

I am trying to write a function that shows whether there are consecutive 8s in a number. I have two different examples of this function. I have included the code and a ss from pythontutor to help visualize the environment diagram.

Function 1

def adj_eight(x):
 if (x % 10) == 8 and (x % 10) == (x // 10 % 10):
    print("True")
    return True
 elif x < 10:
    print("False")
    return False
 else:
    adj_eight(x // 10)

In this function, I do not see the return values on the screen, but the print values do show up. The only time the return value shows up is when the last 2 digits of the number are consecutive 8s (eg 2288) Here is the environment diagram enter image description here

Function 2

def has_adjacent_8s(n):
 last_digit = None # Initialize last_digit to None
 while n > 0:
    digit = n % 10 # Get the last digit of n
    n //= 10 # Remove the last digit from n
    
    if digit == 8 and last_digit == 8:
        return True
    
    last_digit = digit # Update last_digit for the next iteration

 return False

This function does show the return value of True/False when executed. Here is the environment diagram enter image description here

Why is that and what is the difference between the 2? I don't understand why the first one doesn't show the return statements.

Aryan V
  • 111
  • 2
  • 9

1 Answers1

0

You are not returning anything at the end of your function. You need to change the last line to:

    return adj_eight(x // 10)
jprebys
  • 2,469
  • 1
  • 11
  • 16