Python Forum
Define a range, return all numbers of range that are NOT in csv data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Define a range, return all numbers of range that are NOT in csv data
#11
Am I on the right track here?

# Define Site Ranges Dictionary
_number_ranges = {
    "549StKilda"    : {
        'start'     : 61392697000,
        'end'       : 61392697999,
        'name'      : "549 StKilda Rd"
    },
    "Mulgrave"      : {
        'start'     : 61385849100,
        'end'       : 61385849199,
        'name'      : "Mulgrave"
    },
    "Rhodes"      : {
        'start'     : 61298169000,
        'end'       : 61298169999,
        'name'      : "Rhodes"
    },
}

 
# User Input
userinput = input("Enter the Number Yo")
number = int(userinput)

# Define Find Range Function Range 
#def find_range(n, num_range):
 #   if num_range['start'] <= number <= num_range['end']:
  #      print("Number is a " + num_range['name'] + " Number")
   # else:
    #    print("The number is outside the range")

# find_range(number, _range_549stkilda)

# IS number In Range
def in_range(n, num_range):
    for n in num_range.items():  
        if num_range['start'] <= number <= num_range['end']:
            break
        print("Number is a " + num_range['name'] + " Number")

in_range(number,_number_ranges)
Reply
#12
That looks like the right direction, but note that you are double using n as a parameter to in_range and as a for loop variable on line 36. I would really suggest learning to use return values. Here's how I would have done it:

def in_range(n, num_range):
    return num_range['start'] <= number <= num_range['end']

def find_range(n):
    for num_range in _number_ranges:
        if in_range(n):
            break
    return num_range

def check_range():
    userinput = input("Enter the Number Yo")
    num_range = find_range(int(userinput))
    print('{} is a {} number'.format(userinput, num_range['name']
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
Thankyou Ichabod801.

Im going to try and understand your sorcery here.

# Defining function in_range that looks at Dict start and end range and determines if number is in between value. This returns the in_range value within the dictionary. 
def in_range(n, num_range):
    return num_range['start'] <= number <= num_range['end']
# Defining function find_range which performs a for loop for each num_range within the return of in_range and then breaks if true.
def find_range(n):
    for num_range in _number_ranges:
        if in_range(n):
            break
    return num_range

# User input, translated to integer and defined into num_range variable. Then the final output of num_range functions above into 'name'
def check_range():
    userinput = input("Enter the Number Yo")
    num_range = find_range(int(userinput))
    print('{} is a {} number'.format(userinput, num_range['name']
Does my analysis sound correct?

SO now I can define a bunch of number ranges in dict. This would work in a nested Dict?
Reply
#14
Analysis looks correct. A nested dict (just like the one you used in post #11) should work. Note that my code assumed a list of number ranges, and I also messed up the call to in_range. It should be more like this for nested dicts:

def find_range(n):
    for num_range in _number_ranges.values():
        if in_range(n, num_range):
            break
    return num_range
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
So my code is complaining that Line 20 has unused argument for 'n'

# Define Site Ranges Dictionary
_number_ranges = {
    "549StKilda"    : {
        'start'     : 61392697000,
        'end'       : 61392697999,
        'name'      : "549 StKilda Rd"
    },
    "Mulgrave"      : {
        'start'     : 61385849100,
        'end'       : 61385849199,
        'name'      : "Mulgrave"
    },
    "Rhodes"      : {
        'start'     : 61298169000,
        'end'       : 61298169999,
        'name'      : "Rhodes"
    },
}

def in_range(n, num_range):
    return num_range['start'] <= number <= num_range['end']
 
def find_range(n):
    for num_range in _number_ranges.values():
        if in_range(n, num_range):
            break
    return num_range
 
def check_range():
    userinput = input("Enter the Number Yo")
    num_range = find_range(int(userinput))
    print('{} is a {} number'.format(userinput, num_range['name']))
I am trying to work out how you are passing the n argument. Its not actually asking for input so maybe thats why its not passing a value to 'n'.

Line 20 : Unused argument 'n'
If you want to keep the argument and silence this warning, then add comment "#pylint: disable=unused-argument" as first line in your function body.
Line 27 : Using possibly undefined loop variable 'num_range'
It looks like the loop variable (i.e. defined by a for loop or a list comprehension or a generator expression) is used outside the loop.
Reply
#16
Oh, sorry. You need to actually call the check_range function to start the whole thing. And 'number' on line 21 should be 'n' (or 'n' on line 20 should be 'number').

As long as _num_ranges is not empty, num_range will be defined when it gets to line 27. The loop variable num_range is what you are checking with in_range(). If it's valid, the loop breaks, and then what you return is the valid range.

However, come to think of it, there is the possibility that a valid range is not found. In that case, find_range will return the last range, even if it's not valid (because that will be the value after the loop is done). You could fix that with an else:

def find_range(n):
    for num_range in _number_ranges.values():
        if in_range(n, num_range):
            return num_range
    else:
        return None
Returning it within the loop might shut up that warning message you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#17
So if I run this as a file it doesn't ask for input.

Only if I run
check_range() function will it ask for input and return a true value. Which is amazing mind you. Well done.

>>> %Run findrange.py
>>> %Run findrange.py
>>> %Run findrange.py
>>> %Run findrange.py
>>> %Run findrange.py
>>> %Run findrange.py
>>> check_range()
Enter the Number Yo61392697532
61392697532 is a 549 StKilda Rd number
>>>

I also included a catch all to catch all numbers not in range. Im sure there is a better way to do this.

# Define Site Ranges Dictionary
_number_ranges = {
    "549StKilda"    : {
        'start'     : 61392697000,
        'end'       : 61392697999,
        'name'      : "549 StKilda Rd"
    },
    "Mulgrave"      : {
        'start'     : 61385849100,
        'end'       : 61385849199,
        'name'      : "Mulgrave"
    },
    "Rhodes"      : {
        'start'     : 61298169000,
        'end'       : 61298169999,
        'name'      : "Rhodes"
    },
    "NON Ranged"      : {
        'start'     : 0,
        'end'       : 99999999999,
        'name'      : "Not Ranged"
    },
}

def in_range(n, num_range):
    return num_range['start'] <= (n) <= num_range['end']
 
def find_range(n):
    for num_range in _number_ranges.values():
        if in_range(n, num_range):
            return num_range
    else:
        return None
 
def check_range():
    userinput = input("Enter the Number Yo")
    num_range = find_range(int(userinput))
    print('{} is a {} number'.format(userinput, num_range['name']))
Reply
#18
Forgive my stupidity.

I added calling of check_range and it worked a treat.

You are a Guru.
Reply
#19
So no I am on to my next part of this.

Please let me know if you would prefer me to create a new thread. But it is a continuation of this project for me.

I am trying to create a dynamic menu system that will produce menu items based on what items are specified in the _number_ranges dictionary. .

# Define Site Ranges Dictionary
_number_ranges = {
    "549StKilda": {
        'start': 61392697000,
        'end': 61392697999,
        'name': "549 StKilda Rd"
    },
    "Mulgrave": {
        'start': 61385849100,
        'end': 61385849199,
        'name': "Mulgrave"
    },
    "Rhodes": {
        'start': 61298169000,
        'end': 61298169999,
        'name': "Rhodes"
    },
    "Not Defined Number": {
        'start': 0,
        'end': 99999999999,
        'name': "NON Number"
    },

def find_available_n():
    for num_range in _number_ranges:
        print(num_range)
        #Ask for Choice
        choose_range = input("Select Range: ")
        return choose_range
I am trying to create a menu system that will provide number options base of the number of ranges I have defined. Currently my code will only produce the first range.

Since I have defined, StKilda Rd, Mulgrave and Rhodes. I would like the find_available_n function to Print the options

print ("""Select Range:
1. for StKilda Rd
2. for Mulgrave
3. for Rhodes
4. for NON Number""")
Then If I add a another range to the define dictionary of ranges _number_ranges i would like that to be added as well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,442 Jan-05-2024, 08:30 PM
Last Post: sgrey
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,367 May-14-2023, 04:29 PM
Last Post: Winfried
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  How to check multiple columns value within range SamLiu 2 1,103 Mar-13-2023, 09:32 AM
Last Post: SamLiu
  Expand the range of a NumPy array? PythonNPC 0 707 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Copying files if the name is in range tester_V 9 1,476 Nov-24-2022, 12:21 AM
Last Post: tester_V
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,969 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Solving an equation by brute force within a range alexfrol86 3 2,699 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  IndexError: list index out of range dolac 4 1,845 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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