I am confused about arguments and variables


#1

Hello guys, I started doing the course as a complete beginner to programming and there is one pretty confusing thing. It has to do with arguments in this course. I attached a screen shot, and the confusement in on lines 26,27. I understand that after the function there are the arguents in the paratheses. Arguments dont really store any value as variables, so there are there just as identification to which value to attach to each statement in the function ,right ? If this is true, then what I dont understand, if I was to do trip_cost(10,15,10) under the function what it would do exactly ? It would assign values like…10 to city, 15 to day, and 10 to spending_money ? But those functions already have values stored because of the return no ? Also when I do the + spending_money thing, how does python know it is not the variable, and which value would it use ? Would it use a value from here > trip_cost(10,15,10) - if I was to create this ?

Also, if I dont assisn all three arguments, it does not work, how come ? It says this:
trip_cost(‘Los Angeles’, 3, 0) raised an error: unsupported operand type(s) for +: ‘int’ and ‘NoneType’

Thank you for you help guys :))


#2

Well, an example of a correct call to the trip_cost function would be more like …

print trip_cost("Los Angeles", 12, 600)

Then, as the function executes, city would be "Los Angeles", days would be 12, and spending_money would be 600 . When the trip_cost function calls the other functions, values of the specified arguments would be passed to those functions.

The expression you have within the trip_cost function is erroneous, and that led to the error message you received. You must pass days to rental_car_cost, and pass city to plane_ride_cost.

The function has three required parameters in its header, so you need to pass it three arguments to correspond to those parameters.


#3

Sorry for the late reply, ok thank you for making this clear. How ever, what if I would do:
plane_ride_cost(500)
trip_cost(“Los_Angeles”,700,800)

now I would have the same argument with the same value. Since these dont carry anything with them, I think that the rental_car_cost fnction would execute with number 500, but then what would trip_cost do, with 700 assigned ?

Thank you for answering man :slight_smile:


#4

This would return a value of None, since all of the conditions would be False

plane_ride_cost(500)

You need to pass the name of one of the specified cities to the function instead passing it a number. You also may want to print the result of calling the function, for example …

print plane_ride_cost("Pittsburgh")

If you would like to see the result of this function call …

trip_cost("Los_Angeles",700,800)

print it, as follows …

print trip_cost("Los_Angeles",700,800)

However, before doing that, make the necessary correction to the expression within the function. It needs to pass the proper variables to the functions that it calls. See the advice about that in this previous reply.


#5

Once again thank you and sorry for late reply, school was quite hard this week. Ok, I ll look into it and report back.


#6

So I tried all the tips you mentioned, and it seems like I finally understand, thank you very much.
Just one more weird question.

You could say that the function does not hold/store any value in itsself, like a variable does for example right. Because If I do for example - print plane_cost(“Pittsburgh”) it prints the value assosiated with Pittsburgh, but it does not assign any value to the function. Is there a way you can assign a clear value to a function without needing to call it like we were doing during these excercises ? Like If I could assign the Pittsburgh value to the function, and if my code was too long, I would be able to somehow print only the function with the last assosiated value, without putting any argument in the parentheses ?
Thx :smiley:


#7

It is not quite clear to me what you mean, but it appears that what you are asking about is something that cannot be done, except perhaps by using a global variable. The code might get a bit messy though and prone to bugs.


#8

I think you would know if it could be done. Like if I could somehow convert the function to a variable. The value of the function/variable would be the return value.


#9

You could assign the value that the function returned to a variable, for example …

my_pittsburgh_trip = trip_cost("Pittsburgh", 7, 500)

Thereafter you can get the value of the variable elsewhere in the program to use that value for display, a computation, or otherwise.


#10

Ok, these were my questions for now. Thank you for helping.


#11

No problem at all. Be well, and happy coding. :smile:


#12