from random import randint

correct = 0

for i in range(10):
n1 = randint(1,10)
n2 = randint(1,10)
prod = n1 * n2

ans = input('What\'s %d * %d? ' % (n1,n2))
if ans == prod:
print('That\'s Right! Well done.')
correct = correct + 1
else:
print('No, I\'m afraid the answer is %d.' % prod)

print('\nI asked you 10 questions. You got %d of them right.' % correct)
print('Well done!')


----------------------------------------------------------------------------

I wrote this program to try and figure out how everything works. I run the program and it asks me the product of 2 intergers. I type the correct answer and it always jumps to the "else" statement. I am using python 3.2. What am I doing wrong? Any help would be greatly appreciated!

Recommended Answers

All 3 Replies

As far as I can tell there are two things:

1) Your indenting on the post is not correct. I am assuming that this is not in the code, but simply the way you posted it.

2) I think it may be comparing a string with an integer. Where it says

if ans == prod:

change it to

if int(ans) == prod:

Hope this works, I myself am only starting to learn =)

First two meta-issues: Please use [CODE] and [/CODE] tags around your code. The result gives line numbers, saves indentation, highlights keywords and is generally a good thing (and DaniWeb wants you to do it too). The easy way is to press the [CODE] button on at the top of the text box, then paste your code between the tags. You can also highlight your code and then press the button, or just type the tags. The second meta-issue is trivial: Please mention the version of Python you are using near the top of your post: It helps us all get on the right page right away.


And the answer is: You need to cast the input. Compare your line 10 with mine. Without the cast you are comparing the string value from the user to the int value expected. Of course they are not equal. The down side of the cast is that if the user types a non-integer, your program will crash; but that can be fixed as your next exercise. :)

from random import randint

correct = 0

for i in range(10):
    n1 = randint(1,10)
    n2 = randint(1,10)
    prod = n1 * n2

    ans = int(input('What\'s %d * %d? ' % (n1,n2)))
    if ans == prod:
        print('That\'s Right! Well done.')
        correct = correct + 1
    else:
        print('No, I\'m afraid the answer is %d.' % prod)

print('\nI asked you 10 questions. You got %d of them right.' % correct)
print('Well done!')

As an aside: You will find that some people prefer your form of import statement, and some prefer import random followed by a fully scoped use of the item from the imported module, for instance: n1 = random.randint(1,10) There's something to be said for each preference. I prefer the second: If two modules both expose some name, it is too easy to hide one with a later one using the from [I]module[/I] import ... form. Also, looking at code 'far' from the import, it is much easier to understand exactly what e.g. random.randint(1,10) means as opposed to the non-scoped version which might be a function you wrote yourself, for instance. I've been doing this 'programming thing' for quite awhile now, and find that a little extra care early on saves a lot of time (sometimes) later. Self documenting code (such as spelling out the module scope) is worth a little extra effort for me.

Thank you both for your replies. Greatly appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.