How to store multiple strings in python

I have the write a if and else statement for every supposed user using my code in order to make sure they are who they are. I am wondering if there is a more efficient way to do that. What I want the code to look like is line 2 to 7 however currently I have to make it look like line 9 - 14 instead. Is there any way I can make line 2 to 7 work?

I've already tried to separate each user with a separate if and else statement which checks for whether the user exists already and if they get the password that is assigned to that user correct. I've also tried to already use a list function to store multiple different users as one variable so that if the program detects any of those users it moves them onto the next step however once I do that the program refuses to recognize any of the elements of the list as individuals. For example in line 2 of the code neither of the users are recognized. The user blake is only recognized if I separate it in its own if else block.

user_name = input ("Hello, user please enter your username!")
if user_name == ["Jake", "Bake"]:
    Password = input("Please enter your password %s" %user_name)
    if Password == ["hello", "notem"]:
        print ("Welcome back %s" %user_name)
    else:
        print ("You are an imposter! Begone!!")
else:
    if user_name == ("Bake"):
        Password = input("Please enter your password %s" %user_name)
        if Password == ("hell"):
           print ("Welcome back %s" %user_name)
        else:
            print ("You are an imposter! Begone!!")  

Out of line 2 to 7 I expect that I can enter either Jake or even Blake to get the password question. Then once I put the corresponding, and only the corresponding, password then I should be getting a welcome back (which ever username I chose to go with). In reality the program quits on me as soon as I put in any of the usernames because it seems that the program doesn't know how to proceed from the username prompt.

asked Jun 22, 2019 at 9:04

How to store multiple strings in python

if user_name == ["Jake", "Bake"]:

checks whether user_name equals the list ["Jake", "Bake"].

If you want test check whether the name is in the list, use the keyword in:

if user_name in ["Jake", "Bake"]:

answered Jun 22, 2019 at 9:12

DSCDSC

1,1437 silver badges21 bronze badges

1

Your problem is that the program checks to see if the username is the list ["Jake", "Bake"] and the password is the list ["hello", "notem"].

The correct code is:

user_name = input ("Hello, user please enter your username!") if user_name in ["Jake", "Bake"]: Password = input("Please enter your password %s" %user_name) if Password in ["hello", "notem"]: print ("Welcome back %s" %user_name) else: print ("You are an imposter! Begone!!") else: if user_name == ("Bake"): Password = input("Please enter your password %s" %user_name) if Password == ("hell"): print ("Welcome back %s" %user_name) else: print ("You are an imposter! Begone!!")

Edit: you forgot to treat the case in which the user name is neither Jake nor Bake.

answered Jun 22, 2019 at 9:14

Bogdan DoicinBogdan Doicin

2,2725 gold badges26 silver badges32 bronze badges

4

As pointed out by @DSC earlier:
What you've checked in your if statement is whether or not the input given by the user is a list in itself.
Instead what you should be checking is whether the input given by the user is an object inside the list.

Also, note using dictionary to hold { 'Username' : 'Password' } key:values will be helpful to match the username to the exact user's password, in case of multiple users.

users = {'Jake':'hello','Bake':'notem'}

user_name = input ("Hello, Please enter your username: ")

if user_name in users: #Checks whether user exists
    Password = input("Please enter your password %s" %user_name)
    if Password == users[user_name]: #Check for that particular user's password.
        print("Welcome back %s" %user_name)
    else:
        print("You are an imposter! Begone!!")
else:
    print('Sorry, That username doesn't exist.')

Not the safest way to go about, as it tells the potential attackers what exactly they've gotten wrong (The username/Password), But should do your work.

Also check this, should get you a few more steps ahead.

answered Jun 22, 2019 at 9:32

How to store multiple strings in python

Supposing that you want to check for the username and password corresponding to that username, python provides structure called dictionary. May this will be helpful to you.

userPass = {"Jake": "Hello", "Bake": 'notem'} #dictonary named userPass, "username": "Password"

user_name = input ("Hello, user please enter your username!")
Password = input("Please enter your password %s" %user_name)

if userPass[user_name] == Password:  #userPass[user_name] will return password defined in dictonary
    print ("Welcome back %s" %user_name)
else:
    print ("You are an imposter! Begone!!")

answered Jun 22, 2019 at 9:21

How to store multiple strings in python

Harshil ShahHarshil Shah

1421 gold badge3 silver badges13 bronze badges

2

How do I store multiple string values in Python?

Adding Elements in Python Lists.
append() We can append values to the end of the list. We use the append() method for this. ... .
insert() You can insert values in a list with the insert() method. Here, you specify a value to insert at a specific position. ... .
extend() extend() can add multiple items to a list. Learn by example:.

How do you insert multiple strings in Python?

The + operator lets you combine two or more strings in Python. This operator is referred to as the Python string concatenation operator. The + operator should appear between the two strings you want to merge. This code concatenates, or merges, the Python strings “Hello ” and “World”.

How do you store 3 values in Python?

Or, more general solution, create a list containing your 3 variables : my_3_variables = [title, author, date] and then put this list at the first position of your other list : my_list[0] = my_3_variables .