Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 6 – User Input

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 6 – User Input

Spread the love

In the previous lesson we were talking about output. Let’s talk about user input today.

To enter text we use the input() function. It stops the execution of the program until you enter an input from the keyboard and press return. User input is returned as a string:

>>> name = input()     
Sarah

>>> print(name)  
Sarah      

We can use input() with a string parameter which acts as a prompt:

>>> name = input("Enter your name: ")
Enter your name: John

>>> print(name)
John

Even if we enter a number, it is still treated as a string, which means it can’t be directly used in calculations. But it can be concatenated with other strings:

>>> name = input("What's your name?")
What's your name?
Martha

>>> age = input("How old are you, " + name + "?")
How old are you, Martha?
20

>>> print("Oh, I'm " + age + ", too!")
Oh, I'm 20, too!

QUIZ

1. To enter text we use the function:
    A) in()
    B) enter()
    C) input()
 

TRUE OR FALSE?

1) If we enter a number to the input() function, it is treated as a string.

SOLUTION

QUIZ

1. To enter text we use the function:
    A) in()
    B) enter()
    C) input()
   

TRUE OR FALSE?

1) If we enter a number to the input() function, it is treated as a string.
True

PROJECT

'''
User Input - Project

CONVERSATION
__________________________________________________________

Your task is to write a program that will imitate a conversation
between you and the computer. The conversation could look like so:

- Hi, I'm Comp. What's your name?
- Emily
- Hi Emily, where are you from?
- New York
- Hmmm, New York. Nice place. Well, Emily from New York, how old are you?
- 36
- Oh, it sounds impossible. Are you sure you're Emily from New York, aged 36?
- Yes
- My online teacher is a 36-year-old Emily from New York. Is it you???

Naturally, your responses (name, city and age) will be different.


And here's how you should write this program:

#1. Write code that asks the user to enter their name and saves the name
in the variable name.

The prompt should look like this:

- Hi, I'm Comp. What's your name?
-

so make sure that you add a newline character and a dash at the end.

#2. When you run your program, you will enter your name here. And then 
the program should welcome you using your name and ask you to enter
your city. The city should be saved in the variable city.

After the question mark there should be another newline
character and a dash like before.

For example, if your name is Emily, this part should look like so:

- Hi Emily, where are you from?
- 

You should use the name variable in the input prompt and concatenation.

#3. When you run your program, you will enter your city here. And then 
the program should print a message and ask you to enter your age, for
example like this:

- Hmmm, New York. Nice place. Well, Emily from New York, how old are you?
- 

The age should be saved in the variable age. Use concatenation to join
all the parts together. When joining so many parts it's easy to lose track
of all the whitespaces and punctuations, so be careful! In the future you'll
learn better ways of formatting your strings than concatenation.

If your string is too long to fit on one line, you can always span code
over several lines provided it's enclosed in parentheses, square brackets
or curly braces. As you're using the input function, the prompt string
is enclosed in parentheses, so go for it!

#4. When you run your program, you will enter your age here. And then 
the program should print a message and ask a general question, which 
you are supposed to answer with "Yes". This should look like this:

- Oh, it sounds impossible. Are you sure you're Emily from New York, aged 36?
- 

Actually, your answer doesn't matter because it will not be used later code.
So you can use the input function without assigning its result to any
variable.

#5. When you run your program, you will enter "Yes" (or anything else) 
here. And then the program should print a message like this:

- My online teacher is a 36-year-old Emily from New York. Is it you???

'''

PROJECT SOLUTION

##1. 

name = input("- Hi, I'm Comp. What's your name?\n- ")


##2. 

city = input("- Hi " + name + ", where are you from?\n- ")


##3. 

age = input("- Hmm, " + city + ". Nice place. Well, " + name + " from " 
            + city + ", how old are you?\n- ")


##4. 

input("- Oh, it sounds impossible. Are you sure you're "
      + name + " from " + city + ", aged " + age + "?\n- ")


##5. 

print("- My online teacher is a " + age + "-year-old " + name 
      + " from " + city + ". Is it you???")
      

Spread the love

Leave a Reply