I'm trying to get IsInRange() to give me a True or False statement, but the module does not see the main file input. I keep getting
'NameError: name 'firstnum' is not defined'
MODULE1:
def IsInRange():
if firstnum in range(lr,hr) and secondnum in range(lr,hr):
return True
else:
return False
MAIN:
import MODULE1
lr = int(input("Enter your lower range: "))
hr = int(input("Enter your higher range: "))
firstnum = int(input("Enter your first number: "))
secondnum = int(input("Enter your second number: "))
MODULE1.IsInRange()
2 Answers 2
def IsInRange(firstnum, secondnum, lr, hr):
if firstnum in range(lr,hr) and secondnum in range(lr,hr):
return True
else:
return False
Then to use the function, you would pass those values in:
MODULE1.IsInRange(firstnum, secondnum, lr, hr)
EDIT: FYI You also don't need the if else. You can just return the conditional
return (firstnum in range(lr,hr) and secondnum in range(lr,hr))
Example:
def IsInRange(firstnum, secondnum, lr, hr):
return (firstnum in range(lr,hr) and secondnum in range(lr,hr))
>>> IsInRange(6,8,1,10)
True
>>> IsInRange(6,15,1,10)
False
Sign up to request clarification or add additional context in comments.
5 Comments
John Gordon
You're discarding the return result of the function.
degenTy
@JohnGordon not sure if I know what you mean. I meant simply returning the result of the
if conditional is the same behavior, but more concise.John Gordon
Your example call
MODULE1.IsInRange(firstnum, secondnum, lr, hr) does not capture the return result.degenTy
Oh I see what you mean you're right, I was just following along with the question. I'm assuming the OP would either want to
print() or set it as a variable for use later on. result = IsInRange(6,8,1,10)Joshua Hebert
I appreciate everyone's input. This worked for my intended purposes. I am still learning Python and there are a lot of gaps in my knowledge of the basics. Thank you.
def IsInRange(firstnum,secondnum)
if firstnum in range(lr,hr) and secondnum in range(lr,hr):
Comments
lang-py
IsInRange()thats why they are undefinedlower_case_with_underscoresstyle. Is your function just for the sake of example? It could be simplified slightly.