0

I am only starting and getting mad over this function (it gives me the wrong outputs):

def rental_car_cost(days):
 x = 40
 if days < 2:
 return days*x
 elif days >= 3:
 return days*x-20
 elif days>= 7:
 return days*x-50
 else:
 print "Please enter nr of days"

Also, how do I make sure that a number is entered for "days"?

asked Jun 19, 2013 at 16:28
3
  • Give us an example of actual vs expected outputs, and explain how the function is supposed to behave. Also, ask one question per question, please. Commented Jun 19, 2013 at 16:30
  • rental_car_cost(2) should equal 60, but it gives me "please enter nr of days" Commented Jun 19, 2013 at 16:33
  • 3
    @aaa Well, 2 is not less than 2, nor is it greater than or equal to either 3 or 7, so it is only doing what you asked it to... Commented Jun 19, 2013 at 16:37

3 Answers 3

5

Not sure what you are expecting, however change the order of the elif conditions:

def rental_car_cost(days):
 if isinstance(days, int):
 x = 40
 if days < 2:
 return days*x
 elif days >= 7:
 return days*x-50
 elif days>= 3:
 return days*x-20
 else:
 print "Please enter nr of days"
answered Jun 19, 2013 at 16:30
Sign up to request clarification or add additional context in comments.

2 Comments

Consider days = 15, its greater than 7 but will be caught into the previous elif days >= 3: block in your case.
it's because any number greater than 7 is also greater than 3 so it satisfies the condition x >= 3. you want x >= 3 and x < 7
3

The days>= 7 and else clauses never trigger, because the earlier days >= 3 triggers on the same inputs. if/elif/else clauses are processed in order until one of them is triggered.

What you need are clauses for days < 2, days < 7 and else.

To detect non-numbers, start with

if not isinstance(days, int):

which does a type check for integers.

answered Jun 19, 2013 at 16:32

3 Comments

This is python 2.7, so it might be better to do isinstance(days, (int, long)). Although in this context it shouldn't matter I suppose.
@arshajii: buying a car is usually a better idea than renting it for more than 2**31-1 days :)
I was thinking for the sake of completeness, but touché :)
1

rental_car_cost(2) should equal 60

But, none of your if statements will match 2. 2 isn't less than 2, nor is it greater than or equal to 3, nor is it greater than or equal to 7. Follow the advise from the other two answers by larsmans and Ankit Jaiswal also, but I'm assuming 2 should match the days*x-20 part. Just change elif days >= 3: to elif days >= 2:.

answered Jun 19, 2013 at 16:37

2 Comments

thank you. should have spotted that on my own although I was looking for the mistake for some time.
@aaaa: Whenever you look for a mistake for a while, you end up missing it. It happens to me all the time.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.