Member Avatar for leegeorg07

hi again ive been lookin at this code that i found on the internet that test someones mathematical skills i have ironed out most of the errors but i cannot work out this one please help

the code is

name = raw_input("\t\t\tPlease Enter Your Name: ")
print
print "\t\t\t\tHello", name
print "\t\t\tWelcome to my Mathematics Game"

# Selecting the operation of Addition and Substraction followed by the difficulty
print
operation = str(raw_input("\t\t\tAddition (A) or Substraction (S)"))
if operation == "A" or operation == "a":
    difficulty = str(raw_input("\tSelect a difficulty - Easy (E)(1-100), Medium (M) (1-200) or Hard (H) (1-500) \n\nPlease dont try medium or hard as they are still in progress"))
    print
    print "Addition"
    print '** ** **'
    print
if operation == "S" or operation == "s":
    difficulty = str(raw_input("\tSelect a difficulty - Easy (E)(1-100), Medium (M) (1-200) or Hard (H) (1-500) \n\nPlease dont try medium or hard as they are still in progress"))
    print
    print "Subtraction"
    print '** ** ** **'
    print

# Amount of questions asked (10) and randomisation of numbers
import random
counter = 0
while counter < 10:
    counter = counter +1

    # Difficulty - Easy, Medium or Hard
    if difficulty == "Easy" or difficulty == "easy" or difficulty == "E" or difficulty == "e":
        number1 = random.randrange(100) + 1
        number2 = random.randrange(100) + 1
        
    elif difficulty == "Medium" or difficulty == "medium" or difficulty == "M" or difficulty == "m":
        number3 = random.randrange(200) + 1
        number4 = random.randrange(200) + 1
        
    elif difficulty == "Hard" or difficulty == "hard" or difficulty == "H" or difficulty == "h":
        number5 = random.randrange(500) + 1
        number6 = random.randrange(500) + 1

    # Addition Calculations
    if operation == "A" or operation == "a":
            if difficulty == "Easy" or difficulty == "easy" or difficulty == "E" or difficulty == "e":
                print number1, "+", number2, "="
            elif difficulty == "Medium" or difficulty == "medium" or difficulty == "M" or difficulty == "m":
                    print number3, "+", number4, "="
            elif difficulty == "Hard" or difficulty == "hard" or difficulty == "H" or difficulty == "h":
                        print number5, "+", number6, "="
            else:
                # Substraction Calculations
                if operation == "S" or operation == "s":
                    if difficulty == "Easy" or difficulty == "easy" or difficulty == "E" or difficulty == "e":
                        print number1, "-", number2, "="
                elif difficulty == "Medium" or difficulty == "medium" or difficulty == "M" or difficulty == "m":
                        print number3, "-", number4, "="
                else:
                    if difficulty == "Hard" or difficulty == "hard" or difficulty == "H" or difficulty == "h":
                        print number5, "-", number6, "="

    # Input for answer
    answer = int(raw_input("Type in your answer: "))

    # If Its "Correct" or "Incorrect"
    if (answer == number1+number2) or (answer == number3+number4) or (answer == number5+number6) or (answer == number1-number2) or (answer == number3-number4) or (answer == number5-number6):
        print "Correct :)"
        print
    else:
        print "Incorrect :("
        print
print
raw_input("\n\nPress enter to exit.: ")
print

and the error was:
File "C:\Python25\3465.py", line 64, in ?
if (answer == number1+number2) or (answer == number3+number4) or (answer == number5+number6) or (answer == number1-number2) or (answer == number3-number4) or (answer == number5-number6):
NameError: name 'number3' is not defined

Recommended Answers

All 9 Replies

Add the line
number1 = number2 = number3 = number4 = number5 = number6 = 0
right below the line
counter = counter +1

This way your line 64 does not mix up the possible answers and Python knows about all the variables used in that line.

commented: v helpful +1
Member Avatar for leegeorg07

thanks that works now
sorry about something like that :)

edit:

ive found another problem :(
this time its whenever you pick any difficulty from subtraction you will only get four pairs of the star things (sorry about bad explanation) ** (these things)
how can i fix this?
p.s could you please just tell me what i need to do not how to do it so that i can learn myself ??

Firstly, raw_input gives you a string, so you don't need str(), and you also can remove:
" \n\nPlease dont try medium or hard as they are still in progress"
since it is implemented.

Secondly remove the else above
"# Substraction Calculations"
and bring the whole block's indentation level even with Addition Calculations.

There are a whole number of other improvements possible:

operation = raw_input("\t\t\tAddition (A) or Substraction (S): ").capitalize()
difficulty = raw_input("\tSelect a difficulty - Easy (E)(1-100), Medium (M)(1-200) or Hard (H)(1-500): ").capitalize()
if "A" in operation:
    print
    print "Addition"
    print '** ** **'
    print
if "S" in operation:
    print
    print "Subtraction"
    print '** ** ** **'
    print

# that allows you to later use, similar for "M" and "H"
    if "E" in difficulty:
# rather than the awkward
    if difficulty == "Easy" or difficulty == "easy" or difficulty == "E" or difficulty == "e":

There are plenty more problem spots however.
Just work on it, you learn from it, and ultimately the code will look a lot simpler!

Member Avatar for leegeorg07

thanks ill keep looking and if you could when ive finished changing it could you check it to see if theres any way it could be improved?

Member Avatar for leegeorg07

ive got a problem with my new code so far where it says that a colon is an invalid syntax error what is wrong with it?

Can you post your current code so that I can check it? Thanks :)

Member Avatar for leegeorg07

oh yeah sorry here it is:

import random
counter = 0

ae = number1 + number2
am = number3 + number4
ah = number5 + number6
se = number1 - number2
sm = number3 - number4
sh = number5 - number6

while counter < 10:
    counter = counter +1
    number1 = number2 = number3 = number4 = number5 = number6 = 0
name = raw_input("\t\t\tPlease Enter Your Name: ")
ae = number1 + number2
am = number3 + number4
ah = number5 + number6
se = number1 - number2
sm = number3 - number4
sh = number5 - number6
print
print "\t\t\t\tHello", name
print "\t\t\tWelcome to my Mathematics Game"

# Selecting the operation of Addition and Substraction followed by the difficulty
print
operation = raw_input("\t\t\tAddition (A) or Substraction (S)").capitalize()
difficulty = raw_input("\tSelect a difficulty - Easy (E)(1-100), Medium (M) (1-200) or Hard (H) (1-500)").capitalize()
if "A" in operation:
    print "Addition"
    print '** ** **'
if "S" in operation:
    print "Subtraction"
    print '** ** ** **'

if "E" in difficulty:
    number1 = random.randrange(100) + 1
    number2 = random.randrange(100) + 1
        
elif "M"in difficulty:
    number3 = random.randrange(200) + 1
    number4 = random.randrange(200) + 1
        
elif "H" in difficulty:
    number5 = random.randrange(500) + 1
    number6 = random.randrange(500) + 1

    # Addition Calculations
if "A" or "a" in operation:
    if "E" or "e" in difficulty:
        print number1, "+", number2, "="
    elif "M" or "m" in difficulty:
        print number3, "+", number4, "="
    elif "H" or "h" in difficulty:
        print number5, "+", number6, "="
     # Substraction Calculations
if "S" or "s" in operation:
    if "E" or "e" in difficulty:
        print number1, "-", number2, "="
    elif "M" or "m" in difficulty:
        print number3, "-", number4, "="
    elif "H" or "h" in difficulty:
        print number5, "-", number6, "="

    # Input for answer
a = (raw_input("Type in your answer: ")

    
    # If Its "Correct" or "Incorrect"
if (a == ae) or (a == am) or (a == ah) or (a == se) or (a == sm) or (a == sh):
    print "Correct :)"
else:
    print "Incorrect :("


raw_input("\n\nPress enter to exit.: ")
Member Avatar for leegeorg07

with my new code :

import random
counter = 0

while counter < 10:
    counter = counter +1
    number1 = number2 = number3 = number4 = number5 = number6 = 0
name = raw_input("\t\t\tPlease Enter Your Name: ")
print
print "\t\t\t\tHello", name
print "\t\t\tWelcome to my Mathematics Game"

# Selecting the operation of Addition and Substraction followed by the difficulty
print
operation = raw_input("\t\t\tAddition (A) or Substraction (S)").capitalize()
difficulty = raw_input("\tSelect a difficulty - Easy (E)(1-100), Medium (M) (1-200) or Hard (H) (1-500)").capitalize()
if "A" in operation:
    print "Addition"
    print '** ** **'
if "S" in operation:
    print "Subtraction"
    print '** ** ** **'

if "E" in difficulty:
    number1 = random.randrange(100) + 1
    number2 = random.randrange(100) + 1
        
elif "M"in difficulty:
    number3 = random.randrange(200) + 1
    number4 = random.randrange(200) + 1
        
elif "H" in difficulty:
    number5 = random.randrange(500) + 1
    number6 = random.randrange(500) + 1

ae = number1 + number2
am = number3 + number4
ah = number5 + number6
se = number1 - number2
sm = number3 - number4
sh = number5 - number6

    # Addition Calculations
if "A" or "a" in operation:
    if "E" or "e" in difficulty:
        print number1, "+", number2, "="
    elif "M" or "m" in difficulty:
        print number3, "+", number4, "="
    elif "H" or "h" in difficulty:
        print number5, "+", number6, "="
     # Substraction Calculations
if "S" or "s" in operation:
    if "E" or "e" in difficulty:
        print number1, "-", number2, "="
    elif "M" or "m" in difficulty:
        print number3, "-", number4, "="
    elif "H" or "h" in difficulty:
        print number5, "-", number6, "="

    # Input for answer
a = float(raw_input("Type in your answer: "))

    
    # If Its "Correct" or "Incorrect"
if(a==ae)or(a==am)or(a==ah)or(a==se)or(a==sm)or(a==sh):
     print "Correct :)"
else:
    print "Incorrect :("


raw_input("\n\nPress enter to exit.: ")

i have several problems although i dont know how to explain them so please try the code and help :~
it is supposed to ask you 10 questions of varying difficulty from
easy : 0 - 100
medium: 0 - 200
hard: 0 - 500

the questions are for addition or subtraction but not both

thanks in advance

My advice for you is to study up on the very basics of Python. Your code is simply loaded with too many rudimentary mistakes!

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.