1: Hearing


Goal:

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!

Directions:

  1. Create a new file good_listener.py

  2. Add a line calling the built-in input function like: input()

  3. 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.

    1. Type hello in the terminal and press Enter.
    2. Hmm. Nothing happened…? Looking back at our single line, we are explicitly asking Python to accept some input. But we haven’t actually done anything with it! Let’s add instructions to handle this now.
  4. Store the return value from input in a variable named user_input.

    1. You can do this with the assignment operator like user_input = input()
    2. You can run your script again if you like, but still won’t see any change.
  5. Display the value of user_input, to our user via the print function

    1. print(user_input)

    2. 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)
      
  6. 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!”

    1. You can personalize the print statement by using a string concatenation or an f-string:
      1. print("I heard you say, " + user_input + "!")
      2. print(f"I heard you say, {user_input}!")
  7. 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.

2. Listening


Goal:

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
	# ...