RikuSoulz
RikuSoulz

Reputation: 27

Python | Turning a negative number into a positive inside a function that computes operations?

I've been looking here to convert a negative number into positive, I found something but didn't works.. This is an exercise from an online Python page, I'm learning Python. I hope you understand this. This is a trip to Los Angeles, I calculate the money with functions but now there is a problem, I "return from Los Angeles" (I didn't went there, this is just a problem) then the problem says that I spend $2734.23 more and says to calculate the difference between my original money and 2734 in a positive number. When I try to do: print trip_cost("Los Angeles", 5, 600-2734) It's -779 but the problem says that it has to be in a positive number like "779" and I tried to do it on another line but I think that the exercise expects me to do it on the same line that I call the function, I tried everything, I tried abs too, but doesn't works... P.S. They are only asking the difference between that "600" and "2734" but it's too hard for me.. can you please help me and explain it correctly? Im already learning Python and I'm just a noob. Here is the code, and sorry for my bad english...

def hotel_cost(nights): #The nights * 140$
    return nights * 140

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

def rental_car_cost(days): #The car cost
    cost = days * 40
    if days >= 7:
        cost = cost - 50
    elif days >= 3:
        cost = cost - 20
    return cost 

def trip_cost(city, days, spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

# You were planning on taking a trip to LA
# for five days with $600 of spending money.
print trip_cost("Los Angeles", 5, 600)

EDIT: The problem is not solved but I did it on another line like this:

n=-2734
print abs(-n-1955) #This gives 779

and I solved it, but the page didn't said good work, the page keeps saying "It looks like the correct result (779) was not printed." Maybe the page wants me to do another way...? Thank you all for your answers it works on another line but this method I think doesn't works on the printed function's line..

EDIT2:

I solved correctly the problem, the answer was: print 2734.23 - trip_cost("Los Angeles", 5, 600) Thank you very mucho to:Blabla404 and of course the other people that kindly tried to help me!

Upvotes: 0

Views: 258

Answers (1)

user2660581
user2660581

Reputation:

They tell you that you have spend 2734.23, and ask by how much you exceed your theoretical budget (trip_cost("Los Angeles", 5, 600)). So the simplest way is just to print 2734.23 - trip_cost("Los Angeles", 5, 600).

Upvotes: 2

Related Questions