Python splitting string into words and recursion

I am trying to create a code that will accept input (example below)

Input:
BHK158 VEHICLE 11
MOTORCYCLE OYUGH 34.46
BHK158 VEHICLE 12.000
TRIR TRUCK 2.0
BLAS215 MOTORCYCLE 0.001
END

and make a conclusion that lists the numbers of all numbers, and the total cost is indicated next (example below)

Corresponding output:
OIUGHH: 5.8582
BHK158: 5.75
TRIR: 2.666
BLAS215: 0.00017

License plate numbers are charged at $ 0.25 per kilometer (kilometers are listed in the entry list), trucks $ 1.333 per kilometer, and motorcycles $ 0.17 per kilometer. The output is listed in descending order.

Here is my code:

fileinput = input('Input: \n')
split_by_space = fileinput.split(' ')

vehicles = {}


    if split_by_space[1] == 'VEHICLE':
        split_by_space[2] = (float(split_by_space[2]) * 0.25) 
    elif split_by_space[1] == 'TRUCK':
        split_by_space[2] = float(split_by_space[2]) * 1.333 
    elif split_by_space[1] == 'MOTORCYCLE':
        split_by_space[2] = float(split_by_space[2]) * 0.17 

    if split_by_space[0] in vehicles:
        previousAmount = vehicles[split_by_space[0]]
        vehicles[split_by_space[0]] = previousAmount + split_by_space[2]
    else:
        vehicles[split_by_space[0]] = split_by_space[2]

      

Thanks, any help / hints would be greatly appreciated.

+3


source to share


1 answer


Looking through my code I noticed a few things, pointers at the beginning of python start at 0, not 1, so you get a bunch of errors outside. Second, input only enters the first line of the input, so it never goes past the first line. .split()

by default will split the text by \n

, you need to indicate if you want to split something else like a space.

Test.txt content:

BHK158 VEHICLE 11
OIUGHH MOTORCYCLE 34.46
BHK158 VEHICLE 12.000
TRIR TRUCK 2.0
BLAS215 MOTORCYCLE 0.001

      

python code:



fileinput = open('test.txt', 'r')
lines = fileinput.readlines()

vehicles = {}

for line in lines:
    split_by_space = line.split(' ')
    if split_by_space[1] == "VEHICLE":
        split_by_space[2] = (float(split_by_space[2]) * 0.25)
    elif split_by_space[1] == "TRUCK":
        split_by_space[2] = float(split_by_space[2]) * 1.333
    elif split_by_space[1] == "MOTORCYCLE":
        split_by_space[2] = float(split_by_space[2]) * 0.17


    if split_by_space[0] in vehicles:
        previousAmount = vehicles[split_by_space[0]]
        vehicles[split_by_space[0]] = previousAmount + split_by_space[2]
    else:
        vehicles[split_by_space[0]] = split_by_space[2]

      

output:

{'BLAS215': 0.00017, 'OIUGHH': 5.858200000000001, 'TRIR': 2.666, 'BHK158': 5.75}

      

+3


source







All Articles