From eeae761325a93d7dd136455a00a071a9df1114c4 Mon Sep 17 00:00:00 2001 From: Taiyue Tan <30404851+ryotokuro@users.noreply.github.com> Date: Wed, 20 Mar 2019 14:03:43 +1100 Subject: [PATCH] Changes to grammar, content, styling, etc (#28364) --- guide/english/python/input-functions/index.md | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/guide/english/python/input-functions/index.md b/guide/english/python/input-functions/index.md index 59ecaee350..da6338db56 100644 --- a/guide/english/python/input-functions/index.md +++ b/guide/english/python/input-functions/index.md @@ -1,16 +1,19 @@ --- title: Python Input Function --- -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()`. If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. Let's see some examples: +Sometimes in your program you may want some input from the user. This makes the program feel more interactive and versatile to use. In Python 3, to receive input from the user, we use `input()`. When the input function is called, the program flow will stop until the user has given an input and has ended the input with the return key. -1. When we just want to take the input: +--- +Examples +--- +**1\. When we just want to take the input:** - # This will just give a prompt without any message + # This will simply prompt for input, without any indicator/message inp = input() ![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") Run Code -1. To give a prompt with a message: +**2\. To give a prompt together with a message:** prompt_with_message = input('') # _ @@ -18,20 +21,20 @@ Many a time, in a program we need some input from the user. Taking inputs from t ![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") Run Code -3\. When we want to take an integer input: +**3\. When we want to take an integer input:** number = int(input('Please enter a number: ')) ![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") Run Code -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. +If you enter a non integer value then Python will throw an error `ValueError`. This is because it is specified in the program to specifically take an **integer**. **Therefore, be careful to always check if the program is asking for a specific input type.** Otherwise, your program will stop unexpectedly after the prompt. 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: +**4\. When we want a string input:** string = str(input('Please enter a string: ')) @@ -39,22 +42,22 @@ If you enter a non integer value then Python will throw an error `ValueError`. * 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. -5\. Separating input with spaces: +**5\. Separating input with spaces:** ``` a,b,c=input("Please enter 3 words: ").split() ``` ![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":cohete:") [Ejecutar código](https://repl.it/@Brian_RG/multinput) In this case, we can read 3 words separated by space and store in three different vars, so we can use them later. - 6\. Separating input of integers: + **6\. Separating input of integers:** ``` a,b,c=map(int,input("Please insert 3 numbers: ").split()) ``` -As we know, the input are stored by default as a string, so we can use the map() function, indicate that the input will be converted to integers and then store each value in a variable. +By default, inputs are stored as 'strings'. Therefore, we can use the `map()` function to indicate that the input will be converted into integers and then their values are stored in a variable. ![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":cohete:") [Ejecutar código](https://repl.it/@Brian_RG/Intput) [Documentos oficiales](https://docs.python.org/3/library/functions.html#input) -NOTE: Inside the split() function, we can add the separator for the input, por example, if we want to separate each value by a comma, we should write input().split(",") and so on. +NOTE: Inside the `split()` function, we can add the separator used to split and identify chunks of data to be stored separately. For example, if we want to separate each value by a comma, we write `input().split(",")` and so on. Official Docs