2010-10-27 7 views
0

Modifiez la fonction principale pour appeler getAction juste après l'appel de win.getMouse() dans la boucle while. Stockez la valeur de retour dans une variable nommée action et utilisez print pour afficher la valeur renvoyée.Comment stocker une valeur de retour dans une variable et utiliser print pour afficher la valeur renvoyée

Ceci est mon code jusqu'à présent. Ce qui me pose problème, c'est comment retourner la valeur dans une variable nommée action, puis utiliser print pour afficher la valeur renvoyée.

ACTION_PET = 1 
ACTION_FEED = 2 
ACTION_PLAY = 3 
ACTION_IGNORE = 4 
ACTION_ERROR = 5 

def getAction(): 

    win = GraphWin("CS1400 - Pet Dog", 610, 500) 
    clear_screen(win)        
    rec1, rec2, rec3, rec4= draw_buttons(win) 
     while True: 
      mouseClick = win.getMouse()     

      if inBox(rec1, mouseClick): 
       ACTION_PET(win)        
      elif inBox(rec2, mouseClick): 
       ACTION_FEED(win)        
      elif inBox(rec3, mouseClick): 
       ACTION_PLAY(win)       
      elif inBox(rec4, mouseClick): 
       ACTION_IGNORE(win) 
      else: 
       ACTION_ERROR(win) 

       break     


# main program 

def main(): 
    """dog drawing program""" 

    win = GraphWin("CS1400 - Pet Dog", 610, 500)  # create graphics window 
    clear_screen(win)        # start with a clear screen 
    rec1, rec2, rec3, rec4= draw_buttons(win)  # create user buttons 

    # loop forever until cat is dead 
    while True: 
     mouseClick = win.getMouse() 
                # get mouse click 

     if inBox(rec1, mouseClick): 
      drawHappy(win)       # draw happy dog 
     elif inBox(rec2, mouseClick): 
      drawAngry(win)       # draw angry dog 
     elif inBox(rec3, mouseClick): 
      drawSleeping(win)       # draw sleeping dog 
     elif inBox(rec4, mouseClick): 
      drawBored(win)       # draw bored dog 

      break 



    # wait for user to click one more time before ending the program 
    msg_location = Point(305, 430) 
    msg = Text(msg_location, "Click anywhere to quit.") 
    msg.setTextColor("red") 
    msg.draw(win)       # draw message 

    win.close() 
    return 
+0

Veuillez ajouter vos 'import's. Qu'est-ce qui fournit GraphWin, etc? – Daenyth

+0

J'ai essayé de formater le code. Vérifiez si tout va bien. Formatez toujours votre code pour qu'il s'affiche sous forme de code. – pyfunc

+0

@pyfunc: oui c'est correct, merci. –

Répondre

0

Vous avez beaucoup de choses (apparemment) mal avec votre code.

ACTION_PET(win) vous donnera un TypeError: 'int' object is not callable

return the value in a variable named action Quelle valeur?

if inBox(rec1, mouseClick): Où est défini inBox()?


if inBox(rec1, mouseClick): 
      drawHappy(win)       # draw happy dog 
     elif inBox(rec2, mouseClick): 
      drawAngry(win)       # draw angry dog 
     elif inBox(rec3, mouseClick): 
      drawSleeping(win)       # draw sleeping dog 
     elif inBox(rec4, mouseClick): 
      drawBored(win)  

Aucun de ceux-ci n'est défini.

Questions connexes