0

Okay, so this is a bit confusing to explain. Basically, I would like 'num1' to generate a random number every time it is called, however when inside the function, I would like the variable to stay the same, in order to give the correct answer.

#finding random numbers to be used in questions
import random
num1 = int(random.randrange(0,101,1))
num2 = int(random.randrange(0,101,1))


#defining a function that asks an addition question, using random numbers
def addition_question(num1,num2):
    answer1 = num1 + num2
    print("What is",num1,"+",num2,"?")
    given1=input()
    if given1==answer1:
        print("Correct!")
    else:
        print("Sorry, wrong answer!")


print("Question 1:")
addition_question(num1,num2)

I think at the moment what it is doing is assigning a different value for num1 and num2 each time they are called.

Is there any way to solidify the values of the variables within the functions, while still kepping the value of them outside the function random?

Constantly Confused
  • 595
  • 4
  • 10
  • 24
  • It's not clear what you are asking. The only time `num1` and `num2` are assigned values is at the very beginning of the script. The function merely takes two numbers as arguments. Call it multiple times using `num1` and `num2` as the arguments each time, and it will get the same values each time. – chepner Sep 23 '14 at 18:09

3 Answers3

0

A class would be the best bet here.

class NumberQuestion(object):
    def __init__(self):
        self.num1 = random.randrange(0,101,1)
        self.num2 = random.randrange(0,101,1)

    def addition_question(self):
        answer1 = self.num1 + self.num2
        print("What is %s + %s?" % (self.num1, self.num2))
        given1 = input()
        if int(given1) == answer1:
            print("Correct!")
        else:
            print("Sorry, wrong answer!")

print("Question 1:")
question = NumberQuestion()
question.addition_question()
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

You are more than likely comparing an int to a string with if given1 == answer1 so cast the input as an integer, you can also use a while loop to keep asking the user for more guesses until they enter the correct answer.

def addition_question(num1,num2):
    answer1 = num1 + num2
    print("What is",num1, "+",num2,"?")
    while True:
        given1 = int(input())
        if given1 == answer1:
            print("Correct!")
            break
        else:
            print("Sorry, wrong answer, guess again!")
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

This could be a good chance to use your newly acquired closure skills

def question_generator():
    '''returns a function which does the addition stuff'''
    num1 = random.randrange(0,101,1)
    num2 = random.randrange(0,101,1)
    def addition_question():
        answer1 = num1 + num2
        print("What is",num1,"+",num2,"?")
        given1=int(input())
        if given1==answer1:
            print("Correct!")
        else:
            print("Sorry, wrong answer!")
    return addition_question

new_question = question_generator()
print("Question 1:")
new_question()

Note: every time you call question_generator(), a new function is created, which has new num1 and num2. If you call new_question again, it will contain the same num1 and num2

addition_question here is a closure. it "remembers" the scope in which it was created. every time we call question_generator, the scope for the returned function contains two freshly created random numbers. The returned function remembers the value of num1 and num2.

You could call question_generator()() to produce a new addition question if you need not save the returned function.

Community
  • 1
  • 1
srj
  • 9,591
  • 2
  • 23
  • 27