Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 11

Asking questions

I’d like to experience new and amazing kinds of animal sounds, so let’s learn a new function called input. Click run to see what happens!

sound = input("What sound does the animal make?")
if sound == 'meow meow':
  print("It's a cat!")

We can use input to ask the user questions, and the part in quotes is what your prompt is. Then you get to save the answer into a variable!

Try running the code several times, answering with different animal sounds. If you say meow meow Python will tell you it’s a cat, but if you say anything else she won’t!

Let’s practice with input a little bit. The mission:

  • Ask for the user’s name, save it as name
  • Say hello to them, for example “Hello Sam”

  • Hint: No if statement for this one, it’s just practice for input
  • Hint: We’ll use input with “What’s your name?”
  • Hint: Be sure to save it into a variable called name
  • Hint: To ask for the name, we’ll use name = prompt("What's your name?")
  • Hint: We can use print with multiple things by using commas, like print("I ate", 55, "bananas")
  • Hint: print("Hello", name)

Okay, we proved that we’re friendly, so let’s get on to the next chapter and put input to use!

Chapter summary

We learned to ask for input from the user by using input.