This repository was archived by the owner on Apr 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 302
Article: Python Input() Function #1031
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8d7f03a
Create Article: Python Input() Function
meetmangukiya fe04837
Added REPL.it link
meetmangukiya b93aeb2
Implemented comment Suggestions and added rockets after REPL
meetmangukiya 8e84188
Implementing Grammar suggestions
meetmangukiya 0e2a39a
Formatting error fix
meetmangukiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # 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()`. 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) :rocket: | ||
|
|
||
| 2. To give a prompt with a message: | ||
| ```Python | ||
| prompt_with_message = input('<Your prompt message should appear here>') | ||
| # <Your prompt message should appear here> _ | ||
| # The '_' in the output is the prompt | ||
| ``` | ||
| [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) :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' | ||
| ``` | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Give an example snippet for this. |
||
| 4. When we want a string input: | ||
| ```python | ||
| string = str(input('Please enter a string: ')) | ||
| ``` | ||
| [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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can refer to some official documentation here. |
||
|
|
||
| [Official Docs](https://docs.python.org/3/library/functions.html#input) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
give a blank line here, Run code and the next line are in the same line