Python Forum
Fraction Calculation with Limitations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fraction Calculation with Limitations
#8
(Dec-22-2020, 01:27 AM)TINMAN01 Wrote:
(Dec-05-2020, 03:31 PM)deanhystad Wrote: Getting input from a user is something you will use a lot. This is a modified version of a function I wrote so I don't have to keep re-writing user input code over and over. It has been modified to let you specify a list of allowable values.
def input_number(prompt='', values=None, conv=int):
    """Input a number.
    values : An optional list of allowable values
    conv   : The value converter, int() is the default value
    returns the input number
    """
    while True:
        try:
            result = conv(input(prompt))
            if values and not result in values:
                raise ValueError
            return result
        except ValueError:
            print("Input is invalid")
This is the start of a function named "input_number". It takes multiple arguments. Default values are provided for all the arguments making them all optional.
def input_number(prompt='', values=None, conv=int)
This is a docstring. It is a description of the function.
    """Input a number.
    values : An optional list of allowable values
    conv   : The value converter, int() is the default value
    returns the input number
    """
Docstrings aren't required, but I find them useful. If I am using this function later and don't remember the arguments I can just type "help(input_number)" at the Python prompt.
Output:
>>> help(input_number) Help on function input_number in module __main__: input_number(prompt='', values=None, conv=<class 'int'>) Input a number. values : An optional list of allowable values conv : The value converter, int() is the default value returns the input number
I know users will make mistakes typing input, so I give them multiple chances to enter a value and catch when they make an error.
while True:
    try:
        ...
    except ValueError:
        ...
try and except will "catch" errors that occur in the code between the try and except statements. This exception handler catches errors of type "ValueError". Python will raise a ValueError if you call int() and pass it a string that does not represent an integer. For example typing 'a' or '3.14'. The line(s) after "except" is executed when an exception occurs.

This line gets the input and converts it to a number. Instead of using int() to do the conversion I let the caller specify a converter function and provide int() as the default converter.
result = conv(input(prompt))
This code verifies the input value against a list of allowable values. The caller may want to allow all numbers, so this test is only performed if "values" is specified in the function call.
if values and not result in values:
    raise ValueError
If the result is not in the values list the code raises a ValueError. Our exception handler will catch this error.

This is some code I wrote to test the function.
odd_values = [1, 3, 5, 7, 9]
print(input_number(f'Enter number {odd_values}: ', odd_values))
Output:
Enter number [1, 3, 5, 7, 9]: 2 Input is invalid Enter number [1, 3, 5, 7, 9]: a Input is invalid Enter number [1, 3, 5, 7, 9]: 3 3
I think this function, or one like it, could be useful for code that takes user input but has special input limitations.
Reply


Messages In This Thread
Fraction Calculation with Limitations - by TINMAN01 - Dec-05-2020, 02:48 AM
RE: Fraction Calculation with Limitations - by TINMAN01 - Dec-22-2020, 01:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytrends related querie & suggestion limitations cmg15326 0 642 May-02-2023, 03:47 PM
Last Post: cmg15326
  method to remove zero digits from fraction in decimal Skaperen 17 2,980 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  fraction module: can you stop the reducing? qmfoam 1 2,488 Oct-10-2020, 06:10 PM
Last Post: bowlofred
  whole number and fraction Skaperen 11 5,767 Mar-17-2019, 11:33 PM
Last Post: Skaperen
  switch limitations MuntyScruntfundle 3 2,448 Jan-27-2019, 06:11 PM
Last Post: aakashjha001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020