Conditional tests with lists (cont.)
Testing if a value is not in a list
banned_users = ['ann', 'chad', 'dee']
user = 'erin'
if user not in banned_users:
print("You can play!")
Checking if a list is empty
players = []
if players:
for player in players:
print(f"Player: {player.title()}")
else:
print("We have no players yet!")
Letting the user choose when to quit
prompt = "\nTell me something, and I'll "
prompt += "repeat it back to you."
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
prompt = "\nTell me something, and I'll "
prompt += "repeat it back to you."
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
Using break to exit a loop
prompt = "\nWhat cities have you visited?"
prompt += "\nEnter 'quit' when you're done. "
while True:
city = input(prompt)
if city == 'quit':
break
else:
print(f"I've been to {city}!")
More cheat sheets available at
ehmatthes.github.io/pcc_2e/
banned_users = ['eve', 'fred', 'gary', 'helen']
prompt = "\nAdd a player to your team."
prompt += "\nEnter 'quit' when you're done. "
players = []
while True:
player = input(prompt)
if player == 'quit':
break
elif player in banned_users:
print(f"{player} is banned!")
continue
else:
players.append(player)
print("\nYour team:")
for player in players:
print(player)
Accepting input
You can allow your users to enter input using the input()
statement. All input is initially stored as a string.
If you want to accept numerical input, you'll need to
convert the input string value to a numerical type.
name = input("What's your name? ")
print(f"Hello, {name}.")
Accepting numerical input using int()
age = input("How old are you? ")
age = int(age)
if age >= 18:
print("\nYou can vote!")
else:
print("\nYou can't vote yet.")
Accepting numerical input using float()
tip = input("How much do you want to tip? ")
tip = float(tip)
While loops
A while loop repeats a block of code as long as a condition
is True.
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
Accepting input with Sublime Text
Sublime Text doesn't run programs that prompt the user for
input. You can use Sublime Text to write programs that
prompt for input, but you'll need to run these programs from
a terminal.
Avoiding infinite loops
Every while loop needs a way to stop running so it won't
continue to run forever. If there's no way for the condition to
become False, the loop will never stop running. You can
usually press Ctrl-C to stop an infinite loop.
while True:
name = input("\nWho are you? ")
print(f"Nice to meet you, {name}!")
Removing all instances of a value from a list
The remove() method removes a specific value from a list,
but it only removes the first instance of the value you
provide. You can use a while loop to remove all instances
of a particular value.
Removing all cats from a list of pets
pets = ['dog', 'cat', 'dog', 'fish', 'cat',
'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
Breaking out of loops
You can use the break statement and the continue
statement with any of Python's loops. For example you can
use break to quit a for loop that's working through a list or a
dictionary. You can use continue to skip over certain items
when looping through a list or dictionary as well.