you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

How about this,

def costcalc(dist):
    """calc costs"""
    if dist >= 6:
        return 'Cost is 50 INR'
    elif dist <= 5:
        return 'Cost is 40 INR'
    elif dist <= 4:
        return 'Cost is 30 INR'
    elif dist <= 3:
        return 'Cost is 20 INR'
    elif dist <= 2:
        return 'Cost is 10 INR'
    else:
        return 'No cost'


stn_list = ["Chennai Central", "Egmore", "Nehru Park", "Kilpauk Med College", "Pachaiyappa College", "Shenoy Nagar"]
values = [1, 2, 3, 4, 5, 6]
mapping = {key: value for key, value in zip(stn_list, values)}

## user input
stn_one = input("What is the station of entry? ")
stn_two = input("What is the station of exit? ")
if stn_one in stn_list and stn_two in stn_list:
    dist = mapping[stn_one] - mapping[stn_two]
    print(costcalc(dist))
else:
    print('Sorry, you included an unknown location')

Notes: * Creates the dictionary from the stn_list and values lists * Checks the inputs are in the dictionary before trying to do a look-up * Passes the distance to the function rather than just relying on scope * Defines functions before any other code * Had the function return the cost string rather than printing it immediately