-1

I'm trying to solve FizzBuzz using a recursive function (but this isn't what I'm having a problem with yet). I have an if statement that checks if the user typed 'Y' or 'y', but it still runs even if the condition is true and I can't figure out why. I feel like pulling my hair out. The problem is at exit_check.

import sys


final = []


def fizzbuzz(n):
    if n % 5 == 0 and n % 3 == 0:
        print('Append: FizzBuzz')
        final.append('FizzBuzz')
    elif n % 3 == 0:
        print('Append: Fizz')
        final.append('Fizz')
    elif n % 5 == 0:
        print('Append: Buzz')
        final.append('Buzz')
    else:
        print('Append: ' + str(n))
        final.append(n)

    exit_check = input('Exit check: ')

    if exit_check == 'y' or 'Y':
        print('Why does this still get called with the wrong variable?: ' + exit_check)
        sys.exit()

    if n > 0:
        fizzbuzz(n - 1)
    print(final)


fizzbuzz(10)
Phil
  • 33
  • 4

1 Answers1

4
if exit_check == 'y' or 'Y':

this clause is composed of two subclauses, and it returns true if either one of the two conditions is true

subclause 1 is exit_check == 'y'

subclause 2 is 'Y'

In python, a nonempty string is always True, so this compound condition is always True

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38