Trying to creade method .between()

Joined
Jul 20, 2023
Messages
56
Reaction score
2
I know this may not be necessary but im just trying to have a better understanding of classes/modules/methods.
What I want to create is a method like this num.between(low_num, high_num) where the low num does not necessarily need to be provided, if not it wil default to 0. Intergers wil be fine. Dont need to mess with floats for now. Obviously the idea is to check if num is between low_num and high_num.
I havent really had any luck. The closes I've got was with this syntax between(num, low_num, high_num) which I have no real problem getting to work except when I try to make a default argument for low_num. Then python tells me that default arguments must follow positional arguments. Okay I get it... kind of. I think its kind of silly and really ruins the whole idea if I change the placement of the low_num to the end. Then it really doesnt make much sence to me. So anyway please offer any help you can on this. Im really trying to not only solve this small little problem with the positional arguments but more importantly get a better understanding of classes.

Also by the way for the first time chatGTP has really let me down. Actually its been letting me down consistantly on this issue over and over. When I asked it to help me with the default argument issue, at least one time it told me there is no issue with that code. Then at least once it told me there was an issue with it but then sent me back the same exact code that I wrote and told me it was a revised version. Of coarse I also tried re wording things slightly each time to still no avail. Dont know whats up with that but I dont care too much. I prefer the help of my fellow humans anyway.
 
Joined
Jul 4, 2023
Messages
400
Reaction score
44
Have you tried e.g. like this way?
[ on-line ]
Python:
class NumberChecker:
    def between(self, num, low_num=0, high_num=None):
        if high_num is None:
            low_num, high_num = 0, low_num
        return low_num <= num <= high_num


checker = NumberChecker()
print(checker.between(5, 0, 10))  # True
print(checker.between(5, 10))     # True - default low_num is 0
print(checker.between(5, high_num=20))  # True - using keyword argument
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top