Chapter 7: User Input and While Loops Flashcards

1
Q

write a simple message answering a question using the input() function

A

message = input(“Tell me something, and I will repeat it back to you: “)
print(message)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

use a prompt that is more than one line, and then off of that prompt use an input() function

A

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}!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

use int() along with input() to write a number

A

age = input(“How old are you? “)
age = int(age)
print(age)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

use an if-else statement with a prompt, input() and int() functions to make a numerical comparison

A
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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

use an if-else statement with a prompt, input() and int() functions to make a numerical comparison using a modulo operator

A
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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

write a simple while loop that counts up to 5 with the starting number being 1

A

current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is a flag?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

add a flag to a while loop

A
active = True
while active:
    message = input(prompt)
     if message == 'quit':
        active = False
    else:
        print(message)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a break statement?

A

the break statement controls the flow of your program: you can control which lines are executed and which are not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

write a while loop with a break statement

A

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()}!")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does a ‘continue’ statement do?

A

it returns to the beginning of the loop based on the result of a conditional test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

write a while loop with a continue statement using a modulo operator

A
current_number = 0
while current_number <= 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
print(current_number)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

using a while loop move the items of one list to another

A

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())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

use a while loop to remove all instances of a specific value from a list

A

pets = [‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
print(pets)

while ‘cat’ in pets:
pets.remove(‘cat’)

print(pets)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

using a while loop and the input(), fill a dictionary with user input

A

responses = {}

# Set a flag to indicate that polling is active.
polling_active = True
while polling_active:
    # Prompt for the person's name and response.
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")
    # Store the response in the dictionary.
    responses[name] = response
    # Find out if anyone else is going to take the poll.
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False
# Polling is complete. Show the results.
print("\n--- Poll Results ---")
for name, response in responses.items():
    print(f"{name} would like to climb {response}.")