Chapter 7: User Input and While Loops Flashcards
write a simple message answering a question using the input() function
message = input(“Tell me something, and I will repeat it back to you: “)
print(message)
use a prompt that is more than one line, and then off of that prompt use an input() function
prompt = “If you tell us who you are, we can personalize the messages you see.”
prompt += “\nWhat is your first name? “
name = input(prompt)
print(f”\nHello, {name}!”)
use int() along with input() to write a number
age = input(“How old are you? “)
age = int(age)
print(age)
use an if-else statement with a prompt, input() and int() functions to make a numerical comparison
height = input("How tall are you, in inches? ") height = int(height)
if height >= 48:
print(“\nYou’re tall enough to ride!”)
else:
print(“\nYou’ll be able to ride when you’re a little older.”)
use an if-else statement with a prompt, input() and int() functions to make a numerical comparison using a modulo operator
number = input("Enter a number, and I'll tell you if it's even or odd: ") number = int(number)
if number % 2 == 0:
print(f”\nThe number {number} is even.”)
else:
print(f”\nThe number {number} is odd.”)
write a simple while loop that counts up to 5 with the starting number being 1
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
using a simple message from the input() function, add a while loop to allow the user to decide when to stop
also add a line of code that gets rid of the word quit when the user quits
prompt = “\nTell me something, and I will 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)
or while message != 'quit': message = input(prompt) if message == 'quit': print(message)
what is a flag?
a variable that sets a condition for the while loop to keep running. if it’s true, then it would need to be made false
add a flag to a while loop
active = True while active: message = input(prompt) if message == 'quit': active = False else: print(message)
what is a break statement?
the break statement controls the flow of your program: you can control which lines are executed and which are not
write a while loop with a break statement
prompt = “\nPlease enter the name of a city you have visited:”
prompt += “\n(Enter ‘quit’ when you are finished.) “
while True:
city = input(prompt)
if city == 'quit': break else: print(f"I'd love to go to {city.title()}!")
what does a ‘continue’ statement do?
it returns to the beginning of the loop based on the result of a conditional test
write a while loop with a continue statement using a modulo operator
current_number = 0 while current_number <= 10: current_number += 1 if current_number % 2 == 0: continue
print(current_number)
using a while loop move the items of one list to another
Start with users that need to be verified,
# and an empty list to hold confirmed users.
unconfirmed_users = [‘alice’, ‘brian’, ‘candace’]
confirmed_users = []
# Verify each user until there are no more unconfirmed users. # Move each verified user into the list of confirmed users. while unconfirmed_users: current_user = unconfirmed_users.pop()
print(f"Verifying user: {current_user.title()}") confirmed_users.append(current_user)
# Display all confirmed users. print("\nThe following users have been confirmed:") for confirmed_user in confirmed_users: print(confirmed_user.title())
use a while loop to remove all instances of a specific value from a list
pets = [‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
print(pets)
while ‘cat’ in pets:
pets.remove(‘cat’)
print(pets)