python - A variable in my code won't define properly -


this code maths program making high school. struggling @ moment because can't figure out i've done wrong, says sum1 isn't defined. if please take time go through code , sort out grateful.

my error on line: (line 130)

message_to_screen("what is: " + str(sum1) + " + " + str(sum2), black, -100, "medium")

do let me know if need clarify anything.

thanks!

 # below importing modules need import pygame import random import time pygame.locals import *  # initiates pygame pygame.init()  # these defined colours white = (255,255,255) lightgrey = (200,200,200) black = (0,0,0) grey = (100,100,100) red = (255,0,0) yellow = (200,200,0) green = (34,177,76)  # sets display width , height display_width = 800 display_height = 600  gamedisplay = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('major league mathematics')  clock = pygame.time.clock()  fps = 30   easyvalues = list(range(10, 100 + 1)) mediumvalues = list(range(100, 1000 + 1)) hardvalues = list(range(1000, 10000 + 1))  # these set sizes message_to_screen definition smallfont = pygame.font.sysfont("arial", 30) medfont = pygame.font.sysfont("arial", 50) largefont = pygame.font.sysfont("arial", 100) menufont = pygame.font.sysfont("arial", 80)  # next 3 definitions define how text displayed def text_objects(text,color,size):  # 'if' statement     if size == "small":         textsurface = smallfont.render(text, true, color)     elif size == "medium":         textsurface = medfont.render(text, true, color)     elif size == "large":         textsurface = largefont.render(text, true, color)     elif size == "menu":         textsurface = menufont.render(text, true, color)      return textsurface, textsurface.get_rect()  def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "medium"):     textsurf, textrect = text_objects(msg,color,size)     textrect.center = ((buttonx + (buttonwidth / 2)), buttony + (buttonheight / 2))     gamedisplay.blit(textsurf, textrect)  def message_to_screen(msg,color,y_displace = 0,size = "small"):     textsurf, textrect = text_objects(msg,color,size)     textrect.center = (display_width / 2), (display_height / 2) + y_displace     gamedisplay.blit(textsurf, textrect)   def button(text, color, x, y, width, height, inactive_color, active_color, value = none):     cur = pygame.mouse.get_pos()     click = pygame.mouse.get_pressed()      if x + width > cur[0] > x , y + height > cur[1] > y:         pygame.draw.rect(gamedisplay, active_color, (x, y, width, height))         if click[0] == 1 , value != none:             if value == "easy":                 easyvalues = list(range(10, 100 + 1))                 sum1 = random.choice(easyvalues)                 sum2 = random.choice(easyvalues)                 gameloop()             if value == "medium":                 mediumvalues = list(range(100, 1000 + 1))                 sum1 = random.choice(mediumvalues)                 sum2 = random.choice(mediumvalues)                 gameloop()             if value == "hard":                 hardvalues = list(range(1000, 10000 + 1))                 sum1 = random.choice(hardvalues)                 sum2 = random.choice(hardvalues)                 gameloop()             else:                 easyvalues = list(range(10, 100 + 1))                 sum1 = random.choice(easyvalues)                 sum2 = random.choice(easyvalues)                 gameloop()      else:         pygame.draw.rect(gamedisplay, inactive_color, (x, y, width, height))      text_to_button(text, black, x, y, width, height)    def question():     if value == "easy":         sum1 = random.choice(easyvalues)         sum2 = random.choice(easyvalues)     if value == "medium":         sum1 = random.choice(mediumvalues)         sum2 = random.choice(mediumvalues)     if value == "hard":         sum1 = random.choice(hardvalues)         sum2 = random.choice(hardvalues)     else:         sum1 = random.choice(easyvalues)         sum2 = random.choice(easyvalues)     print sum1     print sum2     print sum1 + sum2     print value   #here trying enable raw input user def answer():      answer = ""     font = pygame.font.sysfont("arial", 50)     pygame.draw.rect(gamedisplay, white, [200,250,400,100])       message_to_screen("what is: " + str(sum1) + " + " + str(sum2),                       black,                       -100,                       "medium")     input = true     while input:         event in pygame.event.get():             if event.type == keydown:                 if event.unicode.isdigit():                     answer += event.unicode                 if event.key == k_backspace:                     answer = answer[:-1]   # temporary, while program rest                 elif event.key == k_return:                     if answer == sum1 + sum2:                         message_to_screen("correct!", green, 0, "large")                     else:                         message_to_screen("wrong!", red, 0, "large")                 elif event.key == pygame.k_kp_enter:                     if answer == sum1 + sum2:                         message_to_screen("correct!", green, 0, "large")                     else:                         message_to_screen("wrong!", red, 0, "large")                    elif event.key == k_escape:                     pygame.quit()                     quit()             elif event.type == pygame.quit:                 pygame.quit()                 quit()           block = font.render("answer: " + answer, true, black)         rect = 210,280         gamedisplay.blit(block, rect)         pygame.display.flip()                  ## reference can code working  ##def name(): ##    pygame.init() ##    screen = pygame.display.set_mode((480, 360)) ##    name = "" ##    font = pygame.font.font(none, 50) ##    while true: ##        evt in pygame.event.get(): ##            if evt.type == keydown: ##                if evt.unicode.isalpha(): ##                    name += evt.unicode ##                elif evt.key == k_backspace: ##                    name = name[:-1] ##                elif evt.key == k_return: ##                    name = "" ##            elif evt.type == quit: ##                return ##        screen.fill((0, 0, 0)) ##        block = font.render(name, true, (255, 255, 255)) ##        rect = block.get_rect() ##        rect.center = screen.get_rect().center ##        screen.blit(block, rect) ##        pygame.display.flip()             ##def input_box(): ## ##    pygame.draw.rect(gamedisplay, grey, [(display_width / 2) - 150,(display_height / 2) + 160,300,80]) ##     ## ##    event in pygame.get(): ##        if event.type == pygame.keydown:    # definition defines start screen game doesnt play straight away def startscreen():      menu = true      while menu:  # allows me quit game without problems         event in pygame.event.get():             if event.type == pygame.quit:                 pygame.quit()                 quit()             if event.type == pygame.keydown:                 if event.key == k_escape:                     pygame.quit()                     quit()  # takes away menu screen , starts game once you've pressed 'enter'         if event.type == pygame.keydown:             if event.key == pygame.k_return:                 menu = false          gamedisplay.fill(grey)         message_to_screen("major league mathematics", black, -200, "menu")           #button building         button("easy", black, 100, 220, 150, 100, lightgrey, white, value = "easy")         button("medium", black, 325, 220, 150, 100, lightgrey, white, value = "medium")         button("hard", black, 550, 220, 150, 100, lightgrey, white, value = "hard")          message_to_screen("select difficulty start!", black, 250, "small")         pygame.display.update()    # main loop game run off def gameloop():      gameexit = false     gameover = false         while not gameexit:         event in pygame.event.get():             if event.type == pygame.quit:                 gameexit = true             if event.type == pygame.keydown:                 if event.key == k_escape:                     pygame.quit()                     quit()             gamedisplay.fill(grey)         answer()         pygame.display.update()          clock.tick(fps)      pygame.quit()     quit()   startscreen() 

inside question() function, if value not equal "easy", "medium" or "hard", it'll bump undefined sum1 variable. didn't in line exception raised, best bet.

edit:

ok, reason asked if knew variable scope concepts because see have functions not being called , tries access variables outside scope. suggest further readings variable scoping, immediate answer, following changes in code should work (can't test here):

- in button function

if x + width > cur[0] > x , y + height > cur[1] > y:     pygame.draw.rect(gamedisplay, active_color, (x, y, width, height))     if click[0] == 1 , value != none:         if value == "easy":             easyvalues = list(range(10, 100 + 1))             sum1 = random.choice(easyvalues)             sum2 = random.choice(easyvalues)             gameloop(sum1, sum2)         if value == "medium":             mediumvalues = list(range(100, 1000 + 1))             sum1 = random.choice(mediumvalues)             sum2 = random.choice(mediumvalues)             gameloop(sum1, sum2)         if value == "hard":             hardvalues = list(range(1000, 10000 + 1))             sum1 = random.choice(hardvalues)             sum2 = random.choice(hardvalues)             gameloop(sum1, sum2)         else:             easyvalues = list(range(10, 100 + 1))             sum1 = random.choice(easyvalues)             sum2 = random.choice(easyvalues)             gameloop(sum1, sum2) 

- in gameloop function

def gameloop(sum1, sum2):      gameexit = false     gameover = false      while not gameexit:         event in pygame.event.get():             if event.type == pygame.quit:                 gameexit = true             if event.type == pygame.keydown:                 if event.key == k_escape:                     pygame.quit()                     quit()             gamedisplay.fill(grey)         answer(sum1, sum2)         pygame.display.update()          clock.tick(fps)      pygame.quit()     quit() 

- in answer function

def answer(sum1, sum2): 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -