From 8d7f03a4fb6ff906216fdb8518d136e47410e3a3 Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Wed, 25 May 2016 09:54:44 +0530 Subject: [PATCH 1/5] Create Article: Python Input() Function --- Python-Input.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Python-Input.md diff --git a/Python-Input.md b/Python-Input.md new file mode 100644 index 0000000000..80b379cad4 --- /dev/null +++ b/Python-Input.md @@ -0,0 +1,28 @@ +# Python Input() Function + +Many a times, in a program we need some input from the user. Taking inputs from the user makes the program feel a bit interactive. In python, to take input from the user we have a function `input()`. Let's see some examples: + +1. When we just want to take the input: +```python +# This will just give a prompt without any message +inp = input() +``` + +2. To give a prompt with message: +```Python +prompt_with_message = input('') +# _ +# The '_' in the output is the prompt +``` + +3. When we want to take an integer input: +```Python +number = int(input('Please enter a number: ')) +``` +If you enter a non integer value then Python will throw an error `ValueError`. **So whenever you use this, please make sure that you catch it too.** Otherwise, your program will stop unexpectedly after the prompt. + +4. When we want a string input: +```python +string = str(input('Please enter a string: ')) +``` +Though, inputs are stored by default as a string. Using the `str()` function makes it clear to the code-reader that the input is going to be a 'string'. It is a good practice to mention what type of input will be taken beforehand. From fe0483703654843740509e88d607ad9e29a81720 Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Wed, 25 May 2016 10:10:45 +0530 Subject: [PATCH 2/5] Added REPL.it link --- Python-Input.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Python-Input.md b/Python-Input.md index 80b379cad4..0ae9aa3cf4 100644 --- a/Python-Input.md +++ b/Python-Input.md @@ -7,6 +7,7 @@ Many a times, in a program we need some input from the user. Taking inputs from # This will just give a prompt without any message inp = input() ``` +[Run Code](https://repl.it/CUqX/0) 2. To give a prompt with message: ```Python @@ -14,15 +15,19 @@ prompt_with_message = input('') # _ # The '_' in the output is the prompt ``` +[Run Code](https://repl.it/CUqX/1) 3. When we want to take an integer input: ```Python number = int(input('Please enter a number: ')) ``` -If you enter a non integer value then Python will throw an error `ValueError`. **So whenever you use this, please make sure that you catch it too.** Otherwise, your program will stop unexpectedly after the prompt. +[Run Code](https://repl.it/CUqX/2) +If you enter a non integer value then Python will throw an error `ValueError`. **So whenever you use this, please make sure that you catch it too.** Otherwise, your program will stop unexpectedly after the prompt. 4. When we want a string input: ```python string = str(input('Please enter a string: ')) ``` +[Run Code](https://repl.it/CUqX/3) + Though, inputs are stored by default as a string. Using the `str()` function makes it clear to the code-reader that the input is going to be a 'string'. It is a good practice to mention what type of input will be taken beforehand. From b93aeb2642f3d9c2ddfcfb04f14a44d8820b80eb Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Wed, 25 May 2016 11:16:26 +0530 Subject: [PATCH 3/5] Implemented comment Suggestions and added rockets after REPL --- Python-Input.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Python-Input.md b/Python-Input.md index 0ae9aa3cf4..58ed7ea1ff 100644 --- a/Python-Input.md +++ b/Python-Input.md @@ -1,13 +1,13 @@ # Python Input() Function -Many a times, in a program we need some input from the user. Taking inputs from the user makes the program feel a bit interactive. In python, to take input from the user we have a function `input()`. Let's see some examples: +Many a times, in a program we need some input from the user. Taking inputs from the user makes the program feel a bit interactive. In Python 3, to take input from the user we have a function `input()`. Let's see some examples: 1. When we just want to take the input: ```python # This will just give a prompt without any message inp = input() ``` -[Run Code](https://repl.it/CUqX/0) +[Run Code](https://repl.it/CUqX/0) :rocket: 2. To give a prompt with message: ```Python @@ -15,19 +15,27 @@ prompt_with_message = input('') # _ # The '_' in the output is the prompt ``` -[Run Code](https://repl.it/CUqX/1) +[Run Code](https://repl.it/CUqX/1) :rocket: 3. When we want to take an integer input: ```Python number = int(input('Please enter a number: ')) ``` -[Run Code](https://repl.it/CUqX/2) +[Run Code](https://repl.it/CUqX/2) :rocket: If you enter a non integer value then Python will throw an error `ValueError`. **So whenever you use this, please make sure that you catch it too.** Otherwise, your program will stop unexpectedly after the prompt. +```Python +number = int(input('Please enter a number: ')) +# Please enter a number: as +# Enter a string and it will throw this error +# ValueError: invalid literal for int() with base 10 'as' +``` 4. When we want a string input: ```python string = str(input('Please enter a string: ')) ``` -[Run Code](https://repl.it/CUqX/3) +[Run Code](https://repl.it/CUqX/3) :rocket: Though, inputs are stored by default as a string. Using the `str()` function makes it clear to the code-reader that the input is going to be a 'string'. It is a good practice to mention what type of input will be taken beforehand. + +[Official Docs](https://docs.python.org/3/library/functions.html#input) From 8e841888e760721bfff8f9a16e7de3278228bffa Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Thu, 26 May 2016 09:35:51 +0530 Subject: [PATCH 4/5] Implementing Grammar suggestions --- Python-Input.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python-Input.md b/Python-Input.md index 58ed7ea1ff..56c5cd4556 100644 --- a/Python-Input.md +++ b/Python-Input.md @@ -1,6 +1,6 @@ # Python Input() Function -Many a times, in a program we need some input from the user. Taking inputs from the user makes the program feel a bit interactive. In Python 3, to take input from the user we have a function `input()`. Let's see some examples: +Many a time, in a program we need some input from the user. Taking inputs from the user makes the program feel interactive. In Python 3, to take input from the user we have a function `input()`. Let's see some examples: 1. When we just want to take the input: ```python @@ -9,7 +9,7 @@ inp = input() ``` [Run Code](https://repl.it/CUqX/0) :rocket: -2. To give a prompt with message: +2. To give a prompt with a message: ```Python prompt_with_message = input('') # _ From 0e2a39afa9899dcd26a9a9a29431a6696fa5d870 Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Thu, 26 May 2016 12:50:06 +0530 Subject: [PATCH 5/5] Formatting error fix --- Python-Input.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Python-Input.md b/Python-Input.md index 56c5cd4556..818d772676 100644 --- a/Python-Input.md +++ b/Python-Input.md @@ -22,6 +22,7 @@ prompt_with_message = input('') number = int(input('Please enter a number: ')) ``` [Run Code](https://repl.it/CUqX/2) :rocket: + If you enter a non integer value then Python will throw an error `ValueError`. **So whenever you use this, please make sure that you catch it too.** Otherwise, your program will stop unexpectedly after the prompt. ```Python number = int(input('Please enter a number: '))