[Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

Geoframer geoframer at gmail.com
Fri Jan 19 16:20:59 CET 2007


As i stated i'm somewhat new to python myself so i defined the variables  in
a somewhat old fashion manner. The correct code is below. I also added some
comments because i think you defined the wrong variable somewhere also...
Because your code led to comprimento being compared to comprimento_aeronave
which was never defined anywhere, so i took it you ment comprimento_aeronave
there....

This code below is fully functional without any errors (not even identation
warnings ;-) ). So if this doesn't work at your computer something else is
possible wrong. Incidentally if you hit the following sequence : 1 2 3 4.
The program just terminates without a message... You'd have to insert what's
happening in such a case yourself.

**** insert code ****
global altura_aeronave, largura_aeronave, comprimento_aeronave, comprimento,
\
       largura, altura

def compativel():
    global altura, altura_aeronave, comprimento, comprimento_aeronave, \
           largura, largura_aeronave
    if not largura <= largura_aeronave:
        print 'Volume largo demais!'
    elif not altura <= altura_aeronave:
        print 'Volume alto demais!'
    elif not comprimento<=comprimento_aeronave:
        print 'Volume comprido demais!'

def define():
    global largura, altura, comprimento
    largura=input()
    altura=input()
    comprimento=input()

def porao():
    global altura_aeronave, largura_aeronave, comprimento_aeronave
    porao = input()
    if porao == 1 :
        altura_aeronave = 111
        largura_aeronave = 112
        comprimento_aeronave = 211 #You originally had comprimento here?
        return 1
    elif porao == 4:
        altura_aeronave = 112
        largura_aeronave = 113
        comprimento_aeronave = 212 #Same here
        return 1
    else:
        print "Porao inexistente!"


if porao():
    define()
    compativel()

*** end inserted code ***

HTH - Geoframer

On 1/19/07, Geoframer <geoframer at gmail.com> wrote:
>
> Accidentally the number of function definitions does not help the clarity
> of the program, even though it's completely unclear to me what exactly you
> are trying to do. :-)
> I rewrote your program a bit combining the definition of the functions,
> the comparision of the functions and removed the chance that your program
> goes on the define/compare values that have not been defined.
>
> *** Insert redone code ***
> altura_aeronave = 0
> largura_aeronave = 0
> comprimento_aeronave = 0
> comprimento = 0
> largura = 0
> altura = 0
>
> def compativel():
>     global altura, altura_aeronave, comprimento, comprimento_aeronave, \
>            largura, largura_aeronave
>     if not largura <= largura_aeronave:
>         print 'Volume largo demais!'
>     elif not altura <= altura_aeronave:
>         print 'Volume alto demais!'
>     elif not comprimento<=comprimento_aeronave:
>         print 'Volume comprido demais!'
>
> def define():
>     global largura, altura, comprimento
>     largura=input()
>     altura=input()
>     comprimento=input()
>
> def porao():
>     global altura_aeronave, largura_aeronave, comprimento
>     porao = input()
>     if porao == 1 :
>         altura_aeronave = 111
>         largura_aeronave = 112
>         comprimento = 211
>         return 1
>     elif porao == 4:
>         altura_aeronave = 112
>         largura_aeronave = 113
>         comprimento = 212
>         return 1
>     else:
>         print "Porao inexistente!"
>
>
> if porao():
>     define()
>     compativel()
>
> On 1/19/07, Geoframer <geoframer at gmail.com> wrote:
> >
> > Alright this code is actually littered with bugs, but don't feel bad,
> > because as you state you are new to the whole programming thing. I'll try to
> > explain the things i see going wrong (please take into account that i'm also
> > a new pythonist ;-))
> >
> > ***
> > You get the error 'variable undefined' because you are declaring a
> > global in an incorrect manner. The correct manner is to define the variable
> > in the global scope then start a definition, declare that you are using a
> > global variable and then assign it. Like this :
> >
> > bla = 'Hello world'
> >
> > def func():
> >     global bla
> >     print bla
> > ***
> > If you do :
> > porao = raw_input()
> > You'll define the integer value as a string, not an integer. If you are
> > sure that only an integer will be entered use input() (slower) or
> > int(raw_input()). In either case you should use try and except clauses more
> > in your code because if not a 1 or 4 is entered your code will continue
> > without having the variables declared! Leading to other errors.
> > ***
> > Now suppose you get the definitions using globals correct then you'll
> > get new errors stating that an 'int' is not callable.
> > This happens because you define for example altura as an integer and
> > then also define a function called altura. If you now use :
> > altura()
> > It'll try to call the integer object and will fail.
> > Easy solution would be to make the function something like alturadef()
> > ***
> > As for your identation you should use consistent identation... I suggest
> > using python -t or python -tt to compile your script and it will give
> > warnings/errors where the identation goes wrong.
> > ***
> > Here's how i altered your code (quick and dirty probably, it should be
> > more clean using try and except clauses and what not) to get it to work but
> > you can probably better look that up in a python book and then return to the
> > list if you don't understand it :
> >
> > #Ok,this is supposed to be a 2 option choice between values 1 and 4,
> > #i want the value to determine the variable values inside the function
> >
> > altura_aeronave = 0
> > largura_aeronave = 0
> > comprimento_aeronave = 0
> > comprimento = 0
> > largura = 0
> > altura = 0
> >
> > def porao():
> >     global altura_aeronave, largura_aeronave, comprimento
> >     porao = input()
> >     if porao == 1 :
> >         altura_aeronave = 111
> >         largura_aeronave = 112
> >         comprimento = 211
> >     elif porao == 4:
> >         altura_aeronave = 112
> >         largura_aeronave = 113
> >         comprimento = 212
> >     else:
> >         print "Porão inexistente"
> >
> > #These three functions were supposed to get input from user so it can be
> > compared
> > #with the values determinated(determined?)above
> > def larguradef():
> >     global largura
> >     largura=input()
> >
> > def alturadef():
> >     global altura
> >     altura=input()
> >
> > def comprimentodef():
> >     global comprimento
> >     comprimento = input()
> >
> > #These are the comparison functions
> > def largura_compativel ():
> >     global largura, largura_aeronave
> >     if not largura <= largura_aeronave:
> >         print 'Volume largo demais!'
> >
> > def altura_compativel ():
> >     global altura, altura_aeronave
> >     if not altura <= altura_aeronave:
> >         print 'Volume alto demais!'
> >
> > def comprimento_compativel ():
> >     global comprimento, comprimento_aeronave
> >     if not comprimento<=comprimento_aeronave:
> >         print 'Volume comprido demais!'
> >
> > #Try to run this damn thing,man!!!!!1
> > porao()
> > #print altura_aeronave, largura_aeronave, comprimento
> > larguradef()
> > alturadef()
> > comprimentodef()
> > largura_compativel()
> > altura_compativel
> > comprimento_compativel()
> >
> >
> >
> >
> >
> >
> >
> >
> > On 1/19/07, Karl Wittgenstein < miago.python at gmail.com > wrote:
> >
> > > Dear Smart Caring Dude,
> > > I've been dabbling into Python for about 6 weeks now.I'm a Social
> > > Sciences student who just got interested in programming and chose Python as
> > > first language.I have little time to practice and I am just getting
> > > into  programming concepts,so please be patient,in case you are so kind as
> > > to enlighten this poor soul.
> > > I am trying to write this program which should compare values that are
> > > set by the program  through user's choice to values that the user enters on
> > > a prompt.I use SPE on windows xp,and it tells me that there are
> > > indentation erros on the definitions.Isn't it legal to start a new
> > > block of code when starting a definition?And how come it returns 'variable'
> > > not defined,when they are defined by the = ??Should i make them global?
> > > I would be very grateful to the patient soul that answers these
> > > questions,as my learning interest is sincere and the knowledge sources so
> > > disperse.
> > > Here goes the code:
> > >
> > > #Ok,this is supposed to be a 2 option choice between values 1 and 4,
> > > #i want the value to determine the variable values inside the function
> > > def porao():
> > >     porao = raw_input()
> > >     if porao == 1 :
> > >         global altura_aeronave = 111
> > >         global largura_aeronave = 112
> > >         global comprimento = 211
> > >     elif porao == 4:
> > >        global altura_aeronave = 112
> > >        global largura_aeronave = 113
> > >        global comprimento = 212
> > >     else:
> > >         print "Porão inexistente"
> > > #These three functions were supposed to get input from user so it can
> > > be compared
> > > #with the values determinated(determined?)above
> > > def largura():
> > >    global largura=input()
> > > def altura():
> > >    global altura=input()
> > > def comprimento():
> > >    global comprimento = input()
> > > #These are the comparison functions
> > > def largura_compativel ():
> > >     if not largura <= largura_aeronave:
> > >         print 'Volume largo demais!'
> > > def altura_compativel ():
> > >     if not altura <= altura_aeronave:
> > >         print 'Volume alto demais!'
> > > def comprimento_compativel ():
> > >     if not comprimento<=comprimento_aeronave:
> > >         print 'Volume comprido demais!'
> > > #Try to run this damn thing,man!!!!!1
> > > porao()
> > > largura()
> > > altura()
> > > comprimento()
> > > largura_compativel()
> > > altura_compativel
> > > comprimento_compativel()
> > >
> > >
> > > _______________________________________________
> > > Tutor maillist  -  Tutor at python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> > >
> > >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070119/84dd998f/attachment.htm 


More information about the Tutor mailing list