User Input And While Loops Flashcards

(18 cards)

1
Q

How does the input() function work ?

A

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.

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

What is a prompt ?

A

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

How should the prompt be ?

A

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.

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

Example of What I can do if i have a multi-linen prompt.

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(“\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!

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

When you use input() function, how does python interpret everything the user enters ? How you resolve it ?

A

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.

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

What is the module operator ? How to express it ?
Examples ?

A

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

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

What is the while loop ?

A

It is a loop that runs as long as a certain condition is true.

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

Why are while loops useful ? Example ?

A

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

How to let the user choose when to quit ?

A

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)

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

What is a flag and when to use it ?

A

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.

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

Give an example of a flag

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
12
Q

What is the break statement and when to use it ?

A

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.

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

What is a while true loop ?

A

It is a loop that will run forever u less it reaches a break statement.

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

How and when to use the continue statement ∈ a loop ?

A

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)

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

What can using while loops with lists and dictionaries help me do ?

A

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

How to move items from one list to another ? Give a practical example.

A

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())

17
Q

How to remove all instances of a specific values from a list ?

A

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)

18
Q

How to fill a dictionary with the user input ? Give an example.

A

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 = False

print(“\n— Poll Results —”)
for name, response in responses.items():
print(name + “ would like to climb “ + response + “.”)