Why doe my code not process correctly when I add an extra character and considering all other aspects of programming is correct compared to the comparison in this comparative sample of both mine and the original?


#1

My answer. Note the dollar sign inside the quotes…in my opinion this should not cause a problem. but it does.

def hotel_cost(nights):
  return 140 * nights

def plane_ride_cost(city):
  if city == "Charlotte":
    return "$183"
  elif city == "Tampa":
    return "$220"
  elif city == "Pittsburgh":
    return "$222"
  elif city == "Los Angeles":
    return "$475"

The correct answer:

def hotel_cost(nights):
  return 140 * nights

def plane_ride_cost(city):
  if city == "Charlotte":
    return 183
  elif city == "Tampa":
    return 220
  elif city == "Pittsburgh":
    return 222
  elif city == "Los Angeles":
    return 475

Here is the lesson

Please let me know as soon as possible.


#2

You won’t be able to compute anything from those values.


#3

is it because I have quotes in the return part? I’m struggling undertanding why…


#4

Yes, in Python we can only work with matching types, and for math, that would be numbers.

'$222'

is a string so cannot be added to the other function returns when we go to add them all up.

Numbers should be stored as numbers if we are going to need further use of them. The $ can be added to the output.


#5

what built in function would you use to add that character?

I’m asking because adding a currency operator is ueful in a variety of business operations, applied IT stuff.


#6

If it is in the output, then use a format string.

"Plane ride: $%.2f" % plane_ride_cost('Pittsburgh')

#7

Do I place that after the if - else statement?


#8

For this particular exercise we are not expected to output anything, so, no. Return numbers from that function. Only when putting together the output do we worry about pretty printing and special characters.

As numeric data goes, it should never be converted, but stored as a number. Collect all the data you need to complete a final report and do all the formatting at that stage.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.