0

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()
bcosta12
2,45219 silver badges30 bronze badges
asked Dec 5, 2019 at 18:46
3
  • 1
    You have to pass firstnum and secondnum as arguments to the function IsInRange() thats why they are undefined Commented Dec 5, 2019 at 18:51
  • 1
    Functions don't automatically inherit variables from the caller. As @tpaskowski said, you need to rewrite the function to accept those variables as arguments. Commented Dec 5, 2019 at 18:52
  • Variable and function names should follow the lower_case_with_underscores style. Is your function just for the sake of example? It could be simplified slightly. Commented Dec 5, 2019 at 20:55

2 Answers 2

1
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
answered Dec 5, 2019 at 18:53
Sign up to request clarification or add additional context in comments.

5 Comments

You're discarding the return result of the function.
@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.
Your example call MODULE1.IsInRange(firstnum, secondnum, lr, hr) does not capture the return result.
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)
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.
0

def IsInRange(firstnum,secondnum)

if firstnum in range(lr,hr) and secondnum in range(lr,hr):

answered Dec 5, 2019 at 18:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.