2

How can I re-write this program to avoid using my global num1 and num2? I have an extra credit assignment in my programming class to rewrite a couple of programs without using globals, but this one has me stumped...

# Define the main function
def main():
    randomNumbers()
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

# Define a function to generate random numbers
def randomNumbers():
    import random # Imports the random module

    # Generates two random numbers to be added
    global num1
    global num2
    num1 = random.randrange(100,1000)
    num2 = random.randrange(100,1000)

# Call the main function
main()

Got it figured out! Thank you so much!

# Define the main function
def main():
    num1, num2 = randomNumbers()
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

# Define a function to generate random numbers
def randomNumbers():
    import random # Imports the random module
    rand1 = random.randrange(100,1000)
    rand2 = random.randrange(100,1000)
    return rand1, rand2

# Call the main function
main()
1
  • 1
    put the random calls in the main function and forget about the random function Commented Sep 25, 2014 at 23:17

3 Answers 3

3

If you make your randomNumbers() function return like this, you can avoid global state.

def main():
    # calculate something
    num1, num2 = randomNumbers()
    # calculate something else

def randomNumbers():
    # calculate something
    return num1, num2

Combining two values into one value this way is called a "tuple" if you want to look up more information or documentation.

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

4 Comments

I guess you get the extra credit for this assignment ;-)
@recursive I understand that part... But I can't figure out how to print the return values and check them
@StevenPink: You print and check them exactly the same way you are now. There is no change at all.
Thank you! It finally clicked for me :) I haven't used functions with more than one variable before
1
# Define the main function
def main():
    num1 = randomNumbers()
    num2 = randomNumbers()
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

# Define a function to generate random numbers
def randomNumbers():
    import random # Imports the random module

    # Generates two random numbers to be added
    number = random.randrange(100,1000)
    return number


# Call the main function
main()

Comments

0
def main():
    num1 = random.randrange(100,1000)
    num2 = random.randrange(100,1000)
    print "Please add the following numbers:"
    print "  ", num1
    print "+ ", num2
    print "------"
    correctAnswer = num1 + num2 
    userAnswer = int(raw_input("   "))
    if userAnswer == correctAnswer:
        print "Great job!"
    else:
        # Return the correct answer for the user
        print "You're wrong! The correct answer was %s!" % correctAnswer 

Try that?

1 Comment

That would work, but we're supposed to use functions for computations like that when possible.

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.