Why do my if statements not work in Python?

  if-statement, python

I’m relatively new to Python and I can not figure out for the life of me why this program does not work as intended.

It’s supposed to let the user type either ‘light’ or ‘dark’ and store it in a cookie text file named ‘cookies’ and edit the text in the file if they choose to change it.

When I run it it doesn’t accept the input of ‘light’ or ‘dark’ even though the if statements tell it to, I can’t figure out why.

#Subroutines

#Add Preference Subroutine
def AddPreference():

  #Local Variables
  Cookies = open("cookies.txt", "r+")
  Value = str(Cookies.readline())
  Written = True

  #If the value read from the file is NOT light or dark
  if Value == "":
      Written = False

  #While there is no written value on the line
  while Written == False:
    print("Light or Dark?")
    Preference = str(input())
    Preference = Preference.upper()

    #If the preference is light or dark
    if Preference == "LIGHT" or "DARK":
        Cookies.write(Preference)
        Written = True

    #If the preference isn't light or dark
    if Preference != "LIGHT" or "DARK":
        print("Invalid option.")
        print()
        Written = False
  
  Cookies.close()

#Change Preference Subroutine
def ChangePreference():

  #Local Variables
  Changed = False

  #While there has been no change in preference
  while Changed == False:
    print("Would you like to change your display mode? Y / N")
    Option = str(input(""))
    Option = Option.upper()
    print()

    #If the user wants to change their preference
    if Option == "Y" or "YES":
        Cookies = open("cookies.txt", "w")
        Cookies.write("")
        Cookies.close()
        AddPreference()
        print()
        print("Option changed successfully.")
        Changed = True
    
    #If the user doesn't want to change their preference
    elif Option == "N" or "NO":
        print("Display mode unchanged.")
        Changed = True

    #If the user's input was invalid
    else:
        print("Please type either Y / N.")
        Changed = False

#Main Program

AddPreference()

#Display the current preference
Cookies = open("cookies.txt", "r")

CurrentPreference = str(Cookies.readline())
print("Current display mode: {}".format(CurrentPreference))
print()

Cookies = Cookies.close()

ChangePreference()

Source: Python Questions

LEAVE A COMMENT