How does the input() function work ?
The input() function pauses your program and waits for the user to enter some text. Once Python receives the user’s input, it stores it in a variable to
make it convenient for you to work with.
What is a prompt ?
A prompt is on arguments or a set of instructions that we want to display to the user so they know what to do.
How should the prompt be ?
It has to be clear , easy-to-follow with a space at the end of the prompt to separate the prompt form the user’s response and to make it clear to your user where to enter the text.
Example of What I can do if i have a multi-linen prompt.
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(“\nHello, “ + name + “!”)
Output will be :
If you tell us who you are, we can personalize the messages you see.
What is your first name? Eric
Hello, Eric!
When you use input() function, how does python interpret everything the user enters ? How you resolve it ?
It interprets everything the user enters as a string.
Resolve it By using the int() function which tells python to treat the input as a numerical value.
What is the module operator ? How to express it ?
Examples ?
It is an operator that divides one number by another number and returns the remainder.
Symbol %
7%3=1
5%3=2
10%2=0
What is the while loop ?
It is a loop that runs as long as a certain condition is true.
Why are while loops useful ? Example ?
For example, a game needs a while loop to keep running as long as you want
to keep playing, and so it can stop running as soon as you ask it to quit.
Programs wouldn’t be fun to use if they stopped running before we told
them to or kept running even after we wanted to quit, so while loops are
quite useful.
How to let the user choose when to quit ?
By defining a quit value and then keeping the program running as long as the user has not entered the quit value.
Example :
prompt = “\nTell me something, and I will repeat it back to you:”
prompt += “\nEnter ‘quit’ to end the program. “
message = “”
While message != ‘quit’:
message = input(prompt)
print(message)
What is a flag and when to use it ?
A flag is a variable that determines whether or not the entire program is active as long as many conditions are true. It acts as a signal to the program.
The while loop only checks one condition to be true. What about when there are many different events that could cause the program to stop running ?
Use the flag variable.
We can write our programs so they run while the flag is set to True and stop running when any of several events sets the value of the flag to False.
Give an example of a flag
active = True
while active:
message = input(prompt)
if message == ‘quit’:
active = False
else:
print(message)
What is the break statement and when to use it ?
It is statement that makes one exit a while loop immediately without running any remains code ∈ the loop.
You can use it when you want to control which lines of code have to be executed and which ones do not have to be.
What is a while true loop ?
It is a loop that will run forever u less it reaches a break statement.
How and when to use the continue statement ∈ a loop ?
You use it when you want to return to the beginning of the loop based on the result of a conditional test.
Example :
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
What can using while loops with lists and dictionaries help me do ?
To modify a list as you work through it, use a while loop.
Using while loops with lists and dictionaries allows you to collect, store, and organize lots of input to examine and report on later.
How to move items from one list to another ? Give a practical example.
Verify each user until there are no more unconfirmed users.
One way would be to use a while loop to pull users from the list of unconfirmed users as we verify them and then add them to a separate list of
confirmed users.
Eg:
# Start with users that need to be verified,
# and an empty list to hold confirmed users.
unconfirmed_users = [‘alice’, ‘brian’, ‘candace’]
confirmed_users = []
# Move each verified user into the list of confirmed users.
while unconfirmed_users:
Current_user = unconfirmed_users.pop()
print(“Verifying user: “ + current_user.title())
confirmed_users.append(current_user)
print(“\nThe following users have been confirmed:”)
for confirmed_user in confirmed_users:
print(confirmed_user.title())
How to remove all instances of a specific values from a list ?
You can run a while loop and use the remove function.
Eg:
pets = [‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
print(pets)
while ‘cat’ in pets:
pets.remove(‘cat’)
print(pets)
How to fill a dictionary with the user input ? Give an example.
Store the response in the dictionary:
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? “)
responses[name] = response
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = Falseprint(“\n— Poll Results —”)
for name, response in responses.items():
print(name + “ would like to climb “ + response + “.”)