- Prompt a user to enter an input value and use that value in your program
- Use developer skills to build an understanding of what unfamiliar code is doing
Getting User Input
Our programs haven’t been very exciting so far because we already know what will happen just by looking at the code. What if your program incorporated input from the user?
- Read the code in this repl.it and guess what it will do. (It is also available below, if you prefer to preview it here)
- Run the program. It’s interactive, so be ready to type in your answers in the console area.
- Instead of manually typing in every value, we can collect values from our user to provide a dynamic experience
- When the code is being read, it will stop at
gets.chompand wait for the user to type input into the console
getscollects a string from the console,
chompremoves the final character which is the enter/return
Try It: Getting User Input
Write a small program that asks a user theirs dogs name, then responds with a sentence of your choice! Consider: what variable name will you use to label that user input?
🌶 Finished Early? Click here for a challenge! 🌶
How could you modify this code so that the dogs name is capitalized correctly, no matter how they entered it?
Multiple User Inputs
We know how to get one input from a user. How do we go about getting multiple inputs?
Try It: Getting Multiple User Inputs
Write a program that asks a user for 3 questions. If you're not feeling creative, ask their
age. Output something like the following:
Your name is Amy and you live in Denver. Your are 100 years old.
Try running your program a few times with different values stored in the variables.
🌶 Option to try another Spicy Challenge 🌶
Modify the program so that when you ask the user the second question, you use their first input, etc.