-1
import random

def game(n):
        while counter < n:  
        n1 = random.randrange(1,10)
        n2 = random.randrange(1,10) 
        counter = 0     
        print (str(n1) + " + " + str(n1))
        answer = input("Enter answer: ")
if answer == n1+n2:
    print("Correct.")
else:
    print("Incorrect.")
    counter += 1
    pass

The error is in this line below telling that n1 is not defined.

print (str(n1) + " + " + str(n1))
  • Please check indention. The lines under `while` are not indented. – timrau Sep 04 '16 at 03:19
  • 1
    According to the code you provided, you have indentation issues, and `counter` is not defined either. I'm surprised the first error you are getting on your side is the print statement. – idjaw Sep 04 '16 at 03:19
  • You need to work on your indenting. Make sure it matches up with the code you are running. This sounds like a scope issue, and unless the indenting of the code you post matches your actual code it's hard to help because Indents define scope in Python. Also note there appear to be logic errors in your code as well. – define cindy const Sep 04 '16 at 03:20
  • Please do independent research before asking a question. – TigerhawkT3 Sep 04 '16 at 03:27

1 Answers1

0

Here's what you need to fix:

  1. Your indentation is all over the place.
  2. You were trying use counter as part of your condition in your loop before you even gave it a value.
  3. Because you created n1 and n2 inside of the body of your while loop, after every guess n1 and n2 will be created again and assigned different values. I'm not sure if you wanted that to happen, but if you did you can simply move those 2 lines back inside the loop.
  4. You weren't properly breaking out of your loop if the user guessed the correct answer. That's why I added break. That will end the loop upon a successful answer. Also, you don't need pass if the answer is incorrect. The loop will continue until it receives a correct answer or when it reaches the value of counter.

Anyways, here is a fixed/working version of your code.

import random

def game(n):
    counter = 0
    n1 = random.randrange(1,10)
    n2 = random.randrange(1,10) 
    while counter < n:
        # move creation of n1, n2 here if you want #'s to change after every guess  
        print ('{} + {}'.format(n1, n2))
        answer = input("Enter answer: ")
        if answer == int(n1 + n2):
            print("Correct.")
            break
        else:
            print("Incorrect.")
            counter += 1

You should really read up on some basic Python topics, such as indentation, while loops, and variables.

Hope this helps!

Finally, here are 2 tests. One of which I successfully guess the correct answer and the other I guess until the counter is reached.

4 + 9
Enter answer: 3
Incorrect.
4 + 9
Enter answer: 2
Incorrect.
4 + 9
Enter answer: 1
Incorrect.


9 + 8
Enter answer: 17
Correct.

In both of these examples I called the function with game(3).

Harrison
  • 5,095
  • 7
  • 40
  • 60