This is unbelievably messy. Help


#1



I had to make these functions (pc, hc, rcc,) because I couldn't just add up the other functions into the trip_cost since I decorated it with strings and stuff. I tried to make it work, but yea...


city_one = raw_input("Please select a city to find out cost. Charlotte, Tampa, Pittsburgh, Los Angeles.")

nights = input("For how many nights will you be staying? An integer please:")

r = city_one[0].upper()

city = (r + city_one[1:])


days = input('And for how many days would you like a rental car?')


def plane_ride_cost(city):
     
     if city == "Charlotte":
         print "Plane cost will be:"
         print 183
         return 183 
     
     elif city == "Tampa":
         print "Plane cost will be:"
         print 220
         return 220


     elif city == "Pittsburgh":
         print "Plane cost will be:"
         print 222
         return 222
         
     elif city == "Los Angeles":
         print "Plane cost will be:"
         print 475
         return 475
     
     else :
         return "Error"

def hotel_cost(nights):
    if nights == int(nights):
        print "The hotel cost will be:"
        a = 140 * nights
        print a
        return a 
    
    else:
        return "Error"
 
def rental_car_cost(days):
    
    car_cost = days * 40
    
    if days >= 7:
        print 'rental cost original price will be'
        print car_cost
        car_cost -= 50
        print 'but since it is 7 days or exceeds that limit, $50 will be deducted from the original price shown above. Price changed to:'
        print car_cost 
        return car_cost
    
    elif days >= 3 and days < 7:
        print 'rental cost original price will be'
        print car_cost
        car_cost -= 20
        print 'but since it is 3 days or exceeds that limit, $20 will be deducted from the original price shown above. Price changed to:'
        print car_cost 
        return car_cost
    
    elif days < 3:
        print 'the overall cost for a rental will be'
        print car_cost 
        return car_cost
        
    else :
        
        return 'Error'
        

plane_ride_cost(city)
hotel_cost(nights)
rental_car_cost(days)




def pc():
    if city == 'Charlotte':
      return 183
    
    elif city == 'Tampa':
      return 220
      
    elif city == 'Pittsburgh':
      return 222
      
    elif city == 'Los Angeles':
      return 475
      
    else :
      return 'error'
      
def hc():
  if 0 == 0:
    return nights * 140
    
  else: 
    'error'
    
def rcc():
    
    cc = days * 40

    if days >= 7:
        cc -= 50
        return cc
    
    elif days >= 3 and days < 7:
        cc -= 20
        return cc
    
    elif days < 3:
        return cc
        
    else :
        
        return 'Error'  




def trip_cost():
    if 0 == 0:
      print 'Overall trip cost is:'
      print pc() + hc() + rcc()
        
        
    else :
        print 'error'
     




trip_cost()


#2

You can easily rectify this if you store the value of the total trip cost before printing "Overall trip cost is:"

For example:

def trip_cost():
    totalCost = plane_ride_cost(city) + hotel_cost(nights) + rental_car_cost(days)
    print "The total cost will be: \n$" + str(totalCost) # \n creates a new line, str() converts the int to a string

trip_cost()

This completely eliminates the need for your pc(), hc() and rcc() methods. I'm assuming these are just temporary names by the way, otherwise I would highly recommend you never use potentially confusing acronyms for method names.

As a side note, it's usually better to have methods use variables locally (passed into the method) rather than globally. We could do this using arguments in your method:

def trip_cost(strCity, intNights, intDays):
    totalCost = plane_ride_cost(strCity) + hotel_cost(intNights) + rental_car_cost(intDays)
    print "The total cost will be: \n$" + str(totalCost)

trip_cost(city, nights, days) # passes the values into the method as arguments

This version of the method is more versatile as it allows you to pass in different values for the city, nights and days, should you to need to.


#3

I very much appreciate this! Thank you


#4

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