Python Conditional tests with lists (cont.)
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 player = [] if players: for player in players: print("player:" + player.title()) else: print("we have no players yet!")
Accepting Input
You can allow your users to enter input using the input() statement. In Python 3, all input is stored as a string.
You can allow your users to enter input using the input() statement. In Python 3, all input is stored as a string.
#Simple input name = input ("what's your name? ") print("Hello, " +name + ".") #Accepting numerical input age = input("How old are you? ") age = int(age) if age >= 18: print("\nYou can vote!") else: print("\nYou can't vote yet.")
While loops
A while loop repeats a blocks of code as long as a condition is True.
#Counting to 5 current_number =1 while current_number <= 5: print(current_number) current_number += 1
while loops (cont.)
#Letting the user choose when to quit prompt = "\nTell me something, and I'll " prompt += "repeat it back to you." prompt += "\n Enter 'quit' to end the program. " message = "" while message != 'quit': message = input(prompt) if message != 'quit': print (message) #Using a flag prompt = "\nTell me something, and I'll " prompt += "repeat it back to you." prompt += "\n Enter '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 += "\n Enter 'quit' when you're done. " while True: city = input (prompt) if city == 'quit': break else: print("I've been to " + city + "!")
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.
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.
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.
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.
while loops(cont.)
#Using continue in a loop banned_users = ['eve', 'fred', 'gary', 'helen'] prompt = "\n Add a player to your team." prompt += "\n Enter 'quit' when you're done. " players = [] while True: player = input(prompt) if player == 'quit': break elif player in banned_users: print(player + " is banned!") continue else: players.append(player) print("\nYour team:") for player in players: print(player)
Avoiding infinte 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.
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.
#An infinite loop while True: name = input("\nWho are you? ") print("Nice to meet you, " + name + "!")
Removing all instances of value from a list
The remove() method removes a specific value from a list, but it only removes the value you provide. You can use a while loop to remove all instances of a particular value.
The remove() method removes a specific value from a list, but it only removes 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 pet pets = ['dog', 'cat', 'dog', 'fish', 'cat', 'rabbit', 'cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets)
No comments:
Post a Comment