Pythorial #2 - Input Statement

Now that you have learnt how to make the computer tell you stuff, now you need to learn how to tell the computer stuff (because one-sided conversations are very boring). You can do that with an input statement. The basic syntax is something like this.

variable_name = input("some_string_here")

The input statement allows you to enter any value and returns the same as a string only. You can also add a string inside the input statement's parentheses. This string will be printed before allowing you to input anything. It is useful to let the user know what he/she needs to enter as an input. You can also leave it blank if you don't want anything to be printed.

The value you enter will be stored as a string in a variable (which is represented by variable_name). Now, you can use the variable for anything you want. 

If you need the input to be an integer only, then you could add an int() before the input statement. This int converts the returned string to an integer and returns an error if the entered value is not an integer. The syntax is something like this

variable_name = int(input("some_string_here"))

Now, there are a lot more things you can do with input statements. Do experiment with input and print statements together. 

If you found this post useful, do check out the other posts. The comment section is open for questions and suggestions, so feel free to let your queries known. Do follow the blog if you want to be the first to know when a new post comes out. 

For Pythorial #1: Pythorial #1 - Output Statement (thepygrammer.blogspot.com)

Comments