Section | Video Links |
---|---|
Chain of Responsibility | |
Use Case | |
Python Floor Division | |
Accepting User Input |
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
python ./chain_of_responsibility/chain_of_responsibility_concept.py
Successor1 payload = 1
Successor2 payload = -1
Successor2 payload = -0.5
Successor2 payload = -0.25
Successor1 payload = -0.5
Successor1 payload = 0.5
Successor2 payload = -1.5
Finished result = -1.5
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
python ./chain_of_responsibility/client.py
Enter amount to withdrawal : 180
Dispensing 3 £50 note(s)
Dispensing 1 £20 note(s)
Dispensing 1 £10 note
Now go spoil yourself
Normally division uses a single / character and will return a float even if the numbers are integers or exactly divisible with no remainder,
E.g.,
PS> python
>>> 9 / 3
3.0
Python Version 3 also has an option to return an integer version (floor) of the number by using the double // characters instead.
PS> python
>>> 9 // 3
3
See PEP-0238 : https://www.python.org/dev/peps/pep-0238/
In the file /chain_of_responsibility/client.py above, there is a command input
.
The input
command allows your script to accept user input from the command prompt.
In the ATM example, when you start it, it will ask the user to enter a number.
Then when the user presses the enter
key, the input is converted to an integer and the value tested if valid.
AMOUNT = int(input("Enter amount to withdrawal : "))
if AMOUNT < 10 or AMOUNT % 10 != 0:
...continue
Note that in Python 2.x, use the raw_input()
command instead of input()
.
See PEP-3111 : https://www.python.org/dev/peps/pep-3111/
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.