User Input

Getting Input From the Shell

The input() Function

There's a built-in function in Python called input

Let's Try Some Examples of input()

>>> s = input(">")
>foo
>>> type(s)
<class 'str'>
>>> x = input(">")
>5
>>> type(x)
<class 'str'>
>>> x = int(input(">"))
>5
>>> type(x)
<class 'int'>

The input function does two things:

Write a Program That Asks For a Name

… and then says "hi". Here's the sample output; the text after the > is user input.

What's your name?
>Joe
Hi Joe

A potential solution…

print("What's your name?")
name = input(">\n")
print("Hi " + name)

(note that we used newline, \n, to create a line break for the prompt that's printed out)

Write a Program That Adds Exclamation Points

Here's the sample output; the text after the > is user input.

How loudly?
>4
This is really loud!!!!

A potential solution…

loudly = input("How loudly?\n>")
print("This is really loud" + "!" * int(loudly))

Review

Design, Input, Processing, and Output