Skip to content
Advertisement

Assign value in dictionary to a variable from input [closed]

membership_status = {
's_member' : ['amanda', 'peter', 'alice', 'samuel', 'daniella'],
'not_a_member' : ['micheal', 'thomas', 'victor', 'adrienne', 'limy'],
}
eligible = membership_status['s_member']
not_eligible = membership_status['not_a_member']

username = input("Enter your username")
if username == eligible['']:
     print(f'welcome back {username.title()}')
if username == not_eligible['']:
     print(f'Aww sorry {username.title()}, you are no longer eligible to login')
else:
    print(f'user {username.title()} not found!')

I tried running this code, but it didn’t work.

The code is meant to check the membership status of some individual after inputting their name. How can I make this work?

Advertisement

Answer

You can use in operator. For example:

membership_status = {
    "s_member": ["amanda", "peter", "alice", "samuel", "daniella"],
    "not_a_member": ["micheal", "thomas", "victor", "adrienne", "limy"],
}

username = input("Enter your username: ")

if username in membership_status["s_member"]:
    print(f"welcome back {username.title()}")
elif username in membership_status["not_a_member"]:
    print(f"Aww sorry {username.title()}, you are no longer eligible to login")
else:
    print(f"user {username.title()} not found!")

Prints:

Enter your username: alice
welcome back Alice
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement