User Input

Back to Curriculum Index


  • 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?


  1. Read the code in this and guess what it will do. (It is also available below, if you prefer to preview it here)
  2. 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, the program will stop at gets.chomp and wait for the user to type an input into the console
  • gets collects a string from the console, chomp removes the final character which is the enter/return

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.

Next: Arrays