0

So I have to create a program that asks the user 5 addition questions and they can type the right answer. I am very new at python and functions in general so helpful answers only please. I know how to get random numbers and make it so the question for ex: "What is 4 + 5?" I just do not know how to ask 5 different addition questions within the function. This is what I have.

import random
def add():
    num1=random.randint(1,10)
    num2=random.randint(1,10)
    return num1,num2



def main():
    x,y= add()
    plus=int(input("What is {} + {} ?".format(x,y)))



main()
4
  • When asking about errors, it is important to describe exactly what the error was... please edit your question to add this detail Commented Apr 2, 2015 at 15:44
  • 2
    Not true, there is no error, at least in Python2.7. Please precise which interpreter version are you using. Commented Apr 2, 2015 at 15:46
  • @Nsh: reading the question, there is error, the OP simply doesn't now how to implement the remaining specs. Commented Apr 2, 2015 at 15:47
  • I went back and now there is no error but I am unsure how to make it continue to ask an addition question even if the answer given by the user is right or wrong. Commented Apr 2, 2015 at 15:52

4 Answers 4

1

I don't get an error when I run your code. Here is an answer for you:

Right now your main() is asking for an input back from the user and each time main() is called it will ask for a different number, so if you like you can do something like this:

for _ in range(5):
    main()

But from the sound of it, you want to have the function main() ask all of the questions, namely - something like this:

def main():
    for _ in range(5):
        x,y = add()
        plus = int(input("What is {} + {} ?".format(x,y)))
Sign up to request clarification or add additional context in comments.

Comments

0

Simplest is to use a counting loop:

def main():

    for i in range(5):
         x,y = add()
         plus = int(input("What is {} + {} ?".format(x,y)))

2 Comments

what if I want to tell them the right answer if their answer was incorrect??
@SydneyFranklin: that was not in the original question, and you have already accepted a correct answer. Surely you can figure out a simple if statement yourself? If not, then ask another question.
0

The following program demonstrates how to have a program ask five addition questions:

import random
import sys

def main():
    for count in range(5):
        ask_addition_question()

def ask_addition_question():
    numbers = random.randrange(10), random.randrange(10)
    answer = get_number('What is {} + {}? '.format(*numbers))
    total = sum(numbers)
    if answer == total:
        print('That is correct!')
    else:
        print('Actually, the correct answer is {}.'.format(total))

def get_number(query):
    while True:
        try:
            return int(input(query))
        except KeyboardInterrupt:
            print('Please try again.')
        except EOFError:
            sys.exit()
        except ValueError:
            print('You must enter a number.')

if __name__ == '__main__':
    main()

Comments

0

Just use a for loop to ask the user 5 times

def main():
    for i in range(5):
         x,y = add()
         plus = int(input("What is {} + {} ?".format(x,y)))

To check if the answer is correct just you can do:

 if x + y == plus: print "good"
 else: print "bad"

3 Comments

How would I tell the user of whatever answer they put is right or wrong , and if wrong show the correct answer?
Since it is randomly generated I am confused how to tell the computer to show the right answer if the user inputed the wrong one.
Just have x+y compare to plus, if x+y == plus: "correct"

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.