[Tutor] Why do I not get the same results for these two functions when I pass 7 as an argument?
Nonso Ibenegbu
jollynony at gmail.com
Fri May 3 18:51:31 CEST 2013
Great! Many thanks for the help offered. The background of the problem is
that if I book a certain service for 1 or 2 days I pay $40 per day, if I
book 3, 4, 5 or 6 days I get $20 off but if I book above 7 days I get $50
off.
The first function does the job!
The second function breaks down when the argument is 7 or above.
Yet the only difference is that the condition "if days >= 7:" comes first
in the first function but comes second (as "elif days >= 7:") in the second
code.
I think essentially that the difference lies in the fact that the
conditions were swapped. But how are they different?
On Fri, May 3, 2013 at 8:07 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:
> On 03/05/2013 07:10, Nonso Ibenegbu wrote:
>
>> Hello everyone,
>> Wonder if someone can help me understand why these two codes do not give
>> the same results for what looks essentially the same ("?") code. The
>> argument passed is
>> 7.
>>
>> def rental_car_cost(days):
>> payment = days * 40
>> if days >= 7:
>> return payment - 50
>> elif days >= 3 < 7:
>> return payment - 20
>> else:
>> return payment
>>
>> and...
>>
>> def rental_car_cost(days):
>> payment = days * 40
>> if days >= 3 < 7:
>> return payment - 20
>> elif days >= 7:
>> return payment - 50
>> else:
>> return payment
>>
>>
> Python comparisons are chained. days >= 3 < 7 is saying "days is greater
> or equal to 3 and 3 is less than 7". You need 3 <= days < 7.
>
> --
> If you're using GoogleCrap™ please read this http://wiki.python.org/moin/*
> *GoogleGroupsPython <http://wiki.python.org/moin/GoogleGroupsPython>.
>
> Mark Lawrence
>
> ______________________________**_________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130503/a9f74e74/attachment.html>
More information about the Tutor
mailing list