0

Disclaimer: I am still very new to Python, so if there is an obvious thing that I am missing I apologize, but I couldn't figure it out on my own...

So this is the code I'm working with:

def typeFunct():
  userType= str(raw_input())
  print(userType)
typeFunct()

def main():
  userYn = input("do you wish to type something? (y/n)")
  if (userYn == "y" or "Y"):
    typeFunct()
  if(userYn == "n" or "N"):
    print(" ")
main()

So basically I am working with some code and am trying to use a programmer-created function in order to prompt the user into inputting text that is then printed on the screen, however before I use said function, I am first prompting the user as to whether or not they actually want to type anything with a simple "y/n" input that triggers an if statement:

userYn = input("do you wish to type something? (y/n)")
  if (userYn == "y" or "Y"):
    typeFunct()
  if(userYn == "n" or "N"):
    print(" ")

However though testing I found that the code doesn't seem to actually care if the condition of y/n is true or not, it runs both regardless of the condition given, I also tried using an else: statement:

  else:
    print(" ")

but still no dice, is there like a glaring error in my programming that I am missing that is making if statements act like this?

WIldflower42
  • 83
  • 1
  • 8

1 Answers1

0

I think string expressions just evaluate to true. You should try this:

if (userYn == "y" or userYn == "Y"):
  typeFunct()
if(userYn == "n" or userYn == "N"):
  print(" ")

With if (userYn == "y" or "Y"), you aren't checking if userYn is is "y" or "Y", you are checking if userYn is equal to "y", then checking if the expression after the or evaluates to true, then if any one of them is true, running the code. It's running unconditionally because I think string literals always evaluate to true.