0

I have recently started learning Python, and the experience has been great, however, last night, I tried to make a simple program, and it is acting very strange

I have tried to change the locations of variables, and what they store/handle, and nothing I try seems to be working

def RF():

    user_type = input("Are you a new user? ")

    if user_type is "Yes" or "yes":
        ID = random.randrange(1, 999999)
        print("Your new ID Number is: " + str(ID))
        name = input("Please enter your name: ")
        password = input("Password: ")
        ID = input("Account ID: ")
        writtingID = open('Acc info.txt', 'w')

        writtingID.write("ID: " + str(ID) + " | NAME: " + name + " | PASS: " + password)
        writtingID.close()
    elif user_type is "No" or "no":
        name = input("Please enter your name: ")
        password = input("Password: ")
        ID = input("Account ID: ")


RF()

When the code is ran, I expect it to jump straight to asking for a name, but when I type "No" or "no", it for some reason runs the print("Your new ID Number is: " + str(ID)) from a different if statement

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • `"Yes" or "yes" == "Yes"` – ForceBru Jul 09 '19 at 18:08
  • `if user_type is "Yes" or "yes":` needs to be `if user_type == "Yes" or user_type == "yes":`, otherwise that statement is always true because `"yes"` is Truthy, also `is` is not the same as `==`. – Error - Syntactical Remorse Jul 09 '19 at 18:09
  • `user_type = input("Are you a new user? ").lower()` lets you only compare against `'yes'`. Or you can do `if user_type in ["yes","Yes"]:`Do not use `is` to compare here - use `==` – Patrick Artner Jul 09 '19 at 18:12

2 Answers2

1

    user_type = input("Are you a new user? ")

    if user_type in ("Yes", "yes"):
        ID = random.randrange(1, 999999)
        print("Your new ID Number is: " + str(ID))
        name = input("Please enter your name: ")
        password = input("Password: ")
        ID = input("Account ID: ")
        writtingID = open('Acc info.txt', 'w')

        writtingID.write("ID: " + str(ID) + " | NAME: " + name + " | PASS: " + password)
        writtingID.close()
    elif user_type in ("No", "no"):
        name = input("Please enter your name: ")
        password = input("Password: ")
        ID = input("Account ID: ")


RF()

Check if the word is in a tuple. For an easier approach, just use the str.lower() method to get rid of the extra complexity.

AKA:

if user_type.lower() == "yes":
    code

See how to test multiple variables against a value

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
1

The statement if user_type is "Yes" or "yes": will always return true.

The reason for this is that the statement evaluates the same way as:

if (user_type is "Yes") or ("yes"):

The second parantheses ("yes") will always return true. And as @Error-SyntacticalRemorse pointed out, you want to use == instead of is (see Is there a difference between "==" and "is"?).

Solution 1:

if user_type in ["Yes", "yes"]:

Solution 2:

if user_type.lower() in ["yes", "y"]:
zmike
  • 1,090
  • 10
  • 24