[Tutor] if-elif-else statements

Carroll, Barry Barry.Carroll at psc.com
Fri Oct 14 02:31:42 CEST 2005


Greetings:  

I have two comments to make.   

First, I recommend renaming your variables to be more clear.  For example:

    #####
    speedlimit = input("Please enter the legal speed limit: ") 
    clockedspeed = input("Please enter the clocked speed limit: ")
    excessspeed = clockedspeed - speedlimit
    #####

Next, you really have two questions to answer, the second of which depends
upon the first:

    1. Was the person speeding?
    2. If so, was the person exceeding 90?

I recommend handling this with nested if statements and calculating the fine
only if the person was actually speeding, like this:

    #####
    if excessspeed > 0:
        fine = 50 + 5 * excessspeed 
        if clockedspeed >= 90:
            fine += 200
        print "The total amount due is", fine
    else:
        print "The speed was legal."
    #####

This should give you the results you want.  

BGC

------------------------------

>Date: Thu, 13 Oct 2005 19:13:39 -0400 (EDT)
>From: andrade1 at umbc.edu
>Subject: Re: [Tutor] if-elif-else statements
>To: ewalker at micron.com
>Cc: tutor at python.org
>Message-ID: <1589.172.170.91.105.1129245219.squirrel at 172.170.91.105>
>Content-Type: text/plain;charset=iso-8859-1
>
>amount is supposed to equal fine because there are a couple of different
>fines. there is a fine for going over 90mph that includes a penalty and
>then
>there is a fine for just going over the speed limit as long as it is under
>90.
>
>
>> Is amount suppose to equal total instead of fine?
>>
>>
>>
>> On Thursday 13 October 2005 04:58 pm, andrade1 at umbc.edu wrote:
>>> def main():
>>> ? ? actual = input("Please enter the legal speed limit: ")
>>> ? ? clocked = input("Please enter the clocked speed limit: ")
>>> ? ? speedlimit = clocked - actual
>>>
>>> ? ? fine = 50 + 5*speedlimit
>>> ? ? total = 200 + fine
>>> ? ? amount = fine
>>>
>>> ? ? if speedlimit < 90:
>>> ? ? ? ? print "The total amount due is", amount
>>>
>>> ? ? elif speedlimit >= 90:
>>> ? ? ? ? print "The total amount due is", total
>>>
>>> ? ? else:
>>> ? ? ? ? print "The speed was legal."
>>>
>>>
>>> main()
>>
>>
>>




More information about the Tutor mailing list