Create a Python script which listens for User Input and repeats it back to the user. You can try to do it on your own if you are confident after completing the associated lesson, or follow the recipe below!
Create a new file good_listener.py
Add a line calling the built-in input function like: input()
Run your script from the terminal like python good_listener.py
. Make sure you first navigate to the directory which contains your file! Refer to the Terminal Basics section if needed.
hello
in the terminal and press Enter.Store the return value from input in a variable named user_input.
user_input = input()
Display the value of user_input, to our user via the print function
print(user_input)
Now definitely run your script again! If you type hello, you should see it repeated back after pressing Enter! Here are both lines of our mini-project so far:
# store the result of input function in user_input
user_input = input()
# display the value
print(user_input)
Great! Now let’s do a bit more to be a good listener for our user. Personalize the message instead of just repeating their word back. Your output should be something like: “I heard you say, hello!”
print("I heard you say, " + user_input + "!")
print(f"I heard you say, {user_input}!")
This is as far as we will go for now! Run your script and make any tweaks you deem necessary. Move on to Part 2 after completing the lesson on Loops.
Update your good_listener.py
script to continually accept user input, until the user enters an exit command! (use a while
loop)
exit_commands = ["exit", "quit", "q"]
while True:
choice = input(">> ")
if choice in exit_commands:
print("Thank you for sharing!")
break
# ...