User Input
Goals
- 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?
Directions:
- 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.
Try It: Getting User Input
Write a small program that asks a user their dog's 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 dog's 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 name
, city
, and age
. Output something like the following:
Your name is Kaitlyn and you live in Nashville. Your are 100 years old.
Try running your program a few times with different values stored in the variables.
๐ถ Spicy Challenge ๐ถ
Modify the program so that when you ask the user the second question, you use their first input. Continue that pattern for the other questions in your program!
๐ถ๐ถ Another Spicy Challenge ๐ถ๐ถ
Modify the program so that you ask a yes or no question and change the response based on the user's input! Hint: You will want to use a conditional. You can find more information about conditionals in Ruby here.