1

so I am trying to get a small little addition game working. I have a random number generator working.

    import random


    num1 = gen(10)
    num2 = gen(10)
    answer = int(input('What is', num1, '+', num2))
    print(answer)

I just want the input line to ask the program to ask "what is (random number)+(random number)"

1
  • What is the problem? Random numbers? Errors with input? Commented Aug 11, 2014 at 2:49

5 Answers 5

4

Is this python 3 or python 2?

Regardless of which version of python you are using, input requires a single argument - In this case a string.

You thus need to create a string which contains the numbers. There are several ways to do this:

"What is %s + %s"%(num1, num2)

or

"What is "+str(num1)+" + "+str(num2)

or

"What is {} + {}".format(num1, num2)

Earlier versions of python may not work with the last example, but at least one should be okay.

I would also advise that you enclose your conversion of an input to an int in a try, to prevent users creating exceptions by entering something that isn't an int.

while 1:
    try:
        answer = int(input("What is {} + {}".format(num1, num2))
        break
    except ValueError:
        print "Try again.."

For example with something like that

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

Comments

2
answer = int(input('What is ' + str(num1) + '+' + str(num2)))

Comments

2
answer = int(input('What is %d + %d? ' % (num1, num2)))

3 Comments

Could you please explain your answer?
I suggest you to search for 'python format string'. Basically, this is like printf in other languages.
Add that to your answer, I mean.
2

Try:

answer = raw_input(print("what is %d + %d" (num1, num2))

Comments

1
import random

num1 = gen(10)
num2 = gen(10)

sum = num1    #The sum of your 2 random numbers

correct = False

while not correct:  
    #If they haven't answered correctly, keep asking the question, otherwise move on.
    answer = int(input('What is', num1, '+', num2))

    if answer == sum:
        print("Correct! The answer was: ", answer)
        correct = True
    else:
        print("Incorrect, try again!")

Comments

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.