Mastery 06 – Basic user input

Programs sometimes need to interact with users through the keyboard. In Python 3, the function input() is used for it.

Example 1
Ask for the name of the user and print it:
--> print("Enter your name: ")
    name = input()
    print("Hello " + name)
Enter your name:
Tomer
Hello Tomer

Example 2
You can use the prompt parameter to write a message before the input:
--> name = input("Enter your name: ")
    print("Hello " + name)
Enter your name: Tomer
Hello Tomer

Example 3
The input() function converts the input it receives into a string.  In order to handle input of data type numbers, you may use the function int(). This function tries to convert the string into an integer, otherwise, it throws an exception.
--> age = input("Enter your age: ")
    print("Next year you will be " + (age + 1) + " years old")
Enter your age: 25
Next year you will be 26 years old

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: