Back

Using 'if' with 'in' - Answers

Q1. Get this program working in Python:

super_users = ['David', 'Mary', 'Patrick', 'Majid']

username = input("What is your login? : ")

# Control who can access the system ....

if username in super_users:
    print ("You are authorised to continue.")

else:
    print ("Access denied")

Q2. The program creates a list of authorised users. A user has to enter their name. If they are in the list, they get access otherwise access is denied.
Q3.
If you entered 'david' rather than 'David'?, you will be denied access. Python is case sensitive.
Q4.
To overcome the problem, change

username= input("What is your login? : ")

username.title() = input("What is your login? : ")

There are other options. E.g. you could explore the capitalize() method. you could put the list all in lower or capital letters and then use the lower() or upper() methods to change username.

Q5. Create the following new list and add it to the program:

normal_users = ['Jack', Jill', 'John', 'Jo', ''Jenny']

Q6 - Q8. 

super_users = ['David', 'Mary', 'Patrick', 'Majid']
normal_users = ['Jack', 'Jill', 'John', 'Jo', 'Jenny']
technicians = ['Fred', 'Peter']
username = input("What is your login? : ")

# Control who can access the system ....

if username.title() in super_users:
    print ("Welcome Super User. You are authorised to continue.")

elif username.title() in normal_users:
    print ("Welcome Normal User. You are authorised to continue.")

elif username.title() in technicians:
    print ("Welcome technician. You are authorised to continue.")

else:
    print ("Access denied")

Q9. Write a new program that asks a user what they would like to eat. If their choice is available, then display a suitable message. If it is not available, display a suitable message and ask them to try again, until they enter something in the list. (HINT: You don't need an if construction here. Try using a while construction.)
Q10. Students should have some examples of using if statements with a variety of Boolean operators.

Back