0

I am trying to practice my modular programming with this assignment. I found out how to call the variables into my main function, but for my if statement in evenOdd(), I am not sure how to call the value. I get the error 'randNum' is not defined.

from random import randint
def genNumber():
    randNum = randint(0,20000)
    print("My number is " + str(randNum))
    return (randNum)

def inputNum():
    userNum = input("Please enter a number: ")
    print("Your number is " + str(userNum))
    return (userNum)

def evenOdd():
    if randNum%2==0:
        print("My number is even")
    else:
        print("My number is odd")
    if userNum%2==0:
        print("Your number is even")
    else:
        print("Your number is odd")

def main():
    randNum = genNumber()
    userNum = inputNum()
    evenOdd()
    input("Press ENTER to exit")


main()
4
  • randNum is out of the scope of evenOdd() Commented Oct 3, 2016 at 17:45
  • @MooingRawr How would I edit this to make it in the scope? I'm assuming it will be similar to how I did it in main(), but I am not sure how to go about implementing it. Commented Oct 3, 2016 at 17:47
  • @TannerBritt You can pass its value to the functions that use its value. Commented Oct 3, 2016 at 17:48
  • 1
    @TannerBritt Pass the value as an argument to the function. evenOdd(randNum) Commented Oct 3, 2016 at 17:48

1 Answer 1

2

You can either define randNum in the global scope or just pass it as a variable to your evenOdd() function, like this evenOdd( randNum ).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I originally tried this but got an error for not converting userNum to an int. I stupidly ignored the error thinking it was due to my attempt at a solution.
type casting will get your every time especially in untyped languages. It's far easier to miss the conversion.

Your Answer

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.