[Tutor] Why doesn't this work if I make these changes

Bill Mill bill.mill at gmail.com
Sun Oct 3 06:40:52 CEST 2004


Diana,

Here's what's happening. The PrintStatement function asks Python for
the variable 'limit', and Python tells it that there is none. It
worked in the first code segment only because the variable was defined
globally. What you should do is make the PrintStatement function
accept parameters, like:

def PrintStatement(limit, clocked, speeding, fine, over90, calculatedFine):
    #everything else the same

and then call PrintStatement with those variables, like:

PrintStatement(limit, clocked, speeding, fine, over90, calculatedFine)

What this does is pass the variables limit, clocked, etc. to the
function PrintStatement so that it can use them.

I assume that you're in a novice programming class; to learn more
about parameters (also called arguments) to functions, you should read
ahead in your book. They're pretty simple, but take a while to explain
thoroughly.

Peace
Bill Mill


On Sun, 3 Oct 2004 00:01:10 -0400, Diana Furr <dleigh0 at carolina.rr.com> wrote:
> I'm sorry about that I did forget to put the error.
> 
> This is the error that I get:
> 
> Traceback (most recent call last):
>   File "C:\Python23\lab5-dfurr-cis115.py", line 35, in -toplevel-
>     speedInfo()
>   File "C:\Python23\lab5-dfurr-cis115.py", line 33, in speedInfo
>     printStatement()
>   File "C:\Python23\lab5-dfurr-cis115.py", line 7, in printStatement
>     print "\n","Posted Speed Limit:                 ",limit
> NameError: global name 'limit' is not defined
> 
> 
> 
> ----- Original Message -----
> From: "Bill Mill" <bill.mill at gmail.com>
> To: "Diana Furr" <dleigh0 at carolina.rr.com>
> Cc: <tutor at python.org>
> Sent: Saturday, October 02, 2004 11:30 PM
> Subject: Re: [Tutor] Why doesn't this work if I make these changes
> 
> > Diana,
> >
> > what error does the second program give you? Does it have a semantic
> > error (i.e. not work like you think it should)? Try to make it easy
> > for us to answer your questions, and it'll be easier to help you.
> >
> > Peace
> > Bill Mill
> >
> >
> > ----- Original Message -----
> > From: Diana Furr <dleigh0 at carolina.rr.com>
> > Date: Sat, 2 Oct 2004 23:20:26 -0400
> > Subject: [Tutor] Why doesn't this work if I make these changes
> > To: tutor at python.org
> >
> >
> > This program works the way it is now but I was wanting to make a
> > change. When I do change it, it no longer recognizes some things and I
> > was wondering why. Can someone please explain.
> > This is the program:
> > def printStatement():
> >    print "\n","Posted Speed Limit:                 ",limit
> >    print "\n","Clocked Speed:                      ",clocked
> >    print "\n","Exceeded Speed Limit by:            ",speeding
> >    print "\n","Base fine for speeding:             ",fine
> >    print "\n","Fine for each mph over speed limit: ",speeding*5
> >    print "\n","Fine for exceeding 90 mph:          ",over90
> >    print "\n","Total charges:                      ",calculatedFine
> >
> > limit=input("What is the posted speed limit? ")
> > clocked=input("What was the clocked speed? ")
> > if clocked<=limit:
> >    print "This driver was not speeding "
> > elif clocked>90:
> >    speeding=clocked-limit
> >    fine=50
> >    speedingFine=speeding*5
> >    over90=250
> >    calculatedFine=fine+speedingFine+over90
> >    printStatement()
> > else:
> >    speeding=clocked-limit
> >    fine=50
> >    speedingFine=speeding*5
> >    over90=0
> >    calculatedFine=fine+speedingFine
> >    printStatement()
> > ------------------------------------------------------------------
> > I want to make this change:
> >
> > def speedInfo():
> >    limit=input("What is the posted speed limit? ")
> >    clocked=input("What was the clocked speed? ")
> >    if clocked<=limit:
> >        print "This driver was not speeding "
> >    elif clocked>90:
> >        speeding=clocked-limit
> >        fine=50
> >        speedingFine=speeding*5
> >        over90=250
> >        calculatedFine=fine+speedingFine+over90
> >        printStatement()
> >    else:
> >        speeding=clocked-limit
> >        fine=50
> >        speedingFine=speeding*5
> >        over90=0
> >        calculatedFine=fine+speedingFine
> >        printStatement()
> >
> > speedInfo()
> > ---------------------------------------------------------------------------
> > Someone has told me that ya'll don't like to help with homework. This
> > already does what the assignment
> > asks for but I am playing with it trying to learn more. Thank you for
> > your help.
> > Diana
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
>


More information about the Tutor mailing list