开发者

Variables not working... or i'm missing something?

I am trying to make some edits to a docx file... making a number into letter (i.e. if the variable is equal to 01 = 'one', equal to 02 = 'two', and so on, but in Spanish). The problem is that the variable f_dia_nom doesn't work ... it doesn't even print anything... am I doing something wrong?? or am I missing something??

#!/usr/bin/env python2.6

from Tkinter import *
from docx import *
import tkMessageBox

root = Tk()

nombre = ""
exp_no = ""
ubic = ""
munic = ""
prov = ""
f_dia = ""
f_dia2 = ""
f_dia_nom = ""

def nombre_dia():
         if f_dia2 == 1 or f_dia2 == 01:        
             f_dia_nom = "Un"
         elif f_dia2 == 2 or f_dia2 == 02:               
             f_dia_nom  =  "Dos"
         elif f_dia2 == 3 or f_dia2 == 03:        
             f_dia_nom = "Tres"
         elif f_dia2 == 4 or f_dia2 == 04:               
             f_dia_nom = "Cuatro"
         elif f_dia2 == 5 or f_dia2 == 05:               
             f_dia_nom = "Cinco"
         elif f_dia2 == 6 or f_dia2 == 06:        
             f_dia_nom = "Seis"
         elif f_dia2 == 7 or f_dia2 == 07:               
             f_dia_nom = "Siete"             
         else:
             f_dia_nom = "Error"

# Hacer el docx      
def makedocx():
         if __name__ == '__main__':        
             # Default set of relationshipships - these are the minimum components of a document
             relationships = relationshiplist()

             # estructura del documento
             document = opendocx('test.docx')
             docbody = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]

             # Buscar y reemplazar
             print 'Replacing ...',
             docbody = replace(docbody,'V_EXP',en1.get())            
             docbody = replace(docbody,'V_NOMBRE',en0.get()) 
             docbody = replace(docbody,'V_OPERACION',op.get())           
             docbody = replace(docbody,'V_UBIC',en3.get())
             docbody = replace(docbody,'V_MUNI',en4.get())
             docbody = replace(docbody,'V_PROV',en5.get())
             docbody = replace(docbody,'V_F_DIA',en6.get())
             docbody = replace(docbody,'V_F_MES',mes.get())          
             docbody = replace(docbody,'V_F_SEM',sem.get())
             docbody = replace(docbod开发者_开发问答y,'V_NUM_DIA',en7.get())            
             nombre_dia()
             docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)            
             print 'f_dia_nom'
             print 'done.'


Look up 'global scoping' rules for python. In general, try to avoid global variables as much as possible(if only to avoid errors like these)

in nombre_dia():

f_dia_nom = "Un" 

or any other assignment for that matter has the python compiler create that number LOCAL to the nombre_dia function.

modify your function to declare f_dia_nom as a global:

def nombra_dia():
   global f_dia_nom

   ...

It will have nombre_dia SEE the f_dia_nom as the global

References

Learning Python 4th edition pg 408


f_dia_nom is a global variable, which is not really a great start. If you want to manipulate it, pass f_dia2 to the function nombre_dia() as an argument and then return f_dia_nom from the function afterward.

def nombre_dia(f_dia2):
         if f_dia2 == 1 or f_dia2 == 01:        
             f_dia_nom = "Un"
         elif f_dia2 == 2 or f_dia2 == 02:               
             f_dia_nom  =  "Dos"
         elif f_dia2 == 3 or f_dia2 == 03:        
             f_dia_nom = "Tres"
         elif f_dia2 == 4 or f_dia2 == 04:               
             f_dia_nom = "Cuatro"
         elif f_dia2 == 5 or f_dia2 == 05:               
             f_dia_nom = "Cinco"
         elif f_dia2 == 6 or f_dia2 == 06:        
             f_dia_nom = "Seis"
         elif f_dia2 == 7 or f_dia2 == 07:               
             f_dia_nom = "Siete"             
         else:
             f_dia_nom = "Error"

         return f_dia_nom

if __name__ == '__main__':
    # All your other code...
    f_dia_nom = nombre_dia(f_dia2)
    print 'f_dia_nom =', f_dia_nom
    docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)
    print 'done.'


Get rid of if __name__ == '__main__': That is testing whether or not you are in the main module. (The one you specifically asked to run). I'm guessing that you aren't.

if __name__ == '__main__' only makes sense if you are on the module level (outside of any functions, so you can detect if you are being run or being imported.)


Difficult to see exactly what's supposed to be going on in your code, but a few points:

  1. You're printing the string 'f_dia_nom' rather than the variable f_dia_nom

  2. You can't change global variables from inside a function without using global <variable> first

  3. No need to check against 1 and 01 (etc.) since 1 == 01

  4. You don't seem to be setting/creating f_dia2 anywhere...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜