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()