4/7 : Transportation help


#1

On exercise 4/7 : transportation , i wrote this :

def rental_car_cost(days):
    money = days*40
        if days >= 7:
            money -= 50
        elif days >=3:
            money -=20
        print money
rental_car_cost(1)

Yet the answer is : 40
None
And i got this message :
Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (40) for 1 days.

HELP ME PLEASE !!!!


#2

if days >= 7:

elif days >=3:

here is the problem, check it again

you are close! :slight_smile:


#3

your function doesn't return anything, it only prints.

Please make sure to use the return keyword, and also make suer you have no indention problems


#4

That's not the problem . I tested it and the answer was correct . Because the elif only apply after the if statements are wrong


#5

avoid doing those kinds of ifs

it is bad as a technique.

imagine that 40>=7 which is true
and also 40>=3 is true

meaning that goes to both of them.


#6

Oh..... i see . So "return" will store the datas of a variables and you must use "print" to see the data , right ?

And the task ask me to return it ..... i see .... THANKS !!!!!


#7

I think we should because i makes me code more convinient .
But i think adjust it to this would solve your imagination :

def rental_car_cost(days):
money = days*40
if days >= 7:
money -= 50
elif days >=3 and days <7 :
money -=20
return money
rental_car_cost(days)


#8

Did you solve the issue? And managed to pass the exercise?


#9

Yeah . Thanks
PS : I am trying to elevate the task a bit by letting the users the ability to enter the days :

def days_input():
    days = int(raw_input("Enter the days : "))
    if days == not int():
        print "You did\'t enter a number , try again !"
        days_input()

So as you can see , i don't know how to let the computer to check if the "days" are intergers are not . (Clearly that "if days == not int():" is an error )
Do you know how ?


#10

Did you just fix the code ?


#11

what about:

if not int(days):

you can always check the python docs if you need help with built in functions. you should return the result and the function call if it fails to keep access to the latest value:

def days_input():
    days = int(raw_input("Enter the days : "))
    if not int(days):
        print "You did\'t enter a number , try again !"
        return days_input()
    else:
        return days

why not use a while loop? much better then this recursive function calls


#12

While loop ? What is that function ?


#13

never mind, loops haven't been covered yet. Loops make it easier to just endless prompt the user for input until they give something valid


#14

Well your if not int(days) worked . Now i'm finishing the elevate .

Can we be friends in codeacademy ? You are awsome !


#15

No wait .... "elif not int(days):" is invalid . Sorry . Do you know any solution ?


#16

seems you need try and except:

def days_input():
    try:
        days = int(raw_input("Enter the days : "))
    except ValueError:
        print "You did\'t enter a number , try again !"
        return days_input()
    return days

try and except isn't covered in the course, it allows you to catch error. ValueError is the error return by int() if the input can't converted into a number


#17

So your answer works . But when i tried the code , it said that global "days" is not identified in line 21 and 27 (which are part of the rental_car_cost())

def days_input():
try:
days = int(raw_input("Enter the days : "))
except ValueError:
print "You did\'t enter a number , try again !"
return days_input()
return days
days_input()
def rental_car_cost():
money = days*40
if days >= 7:
money -= 50
elif days >=3 and days <7 :
money -=20
return money
rental_car_cost()


#18

well, i would store the result of days_input in a variable since you are going to need it:

days = days_input()

because your rental car function still need a parameter and argument. I can't do all the coding for you, it is your challenge, not mine


#19

Yeah . I understand that you helped me everything you could do as an Codecademy Moderator . I would not bother you now . I'll figure things out .
Thanks again


#20

It is more that you now reply within 5 minutes, while my answers leave plenty for you to do. Of course, if you really can't figure it out, you can ask :slight_smile: