-1

Now that it has returned the value of the smallest item in list 1, I would like it then display the name of this value on the next line. Can somebody please tell me how to get python to print the name of the smallest value in the list as well as displaying the value of it.

bpPetrol = 099.9
bpDiesel = 100.9

shellPetrol = 102.9
shellDiesel = 103.9

texacoPetrol = 100.9
texacoDiesel = 101.9

gulfPetrol = 098.9
gulfDiesel = 102.9

tescoPetrol = 100.9
tescoDiesel = 102.9

list1= [bpPetrol, shellPetrol, texacoPetrol, gulfPetrol, tescoPetrol]
list2= [bpDiesel, shellDiesel, texacoDiesel, gulfDiesel, tescoDiesel]

if searchRadius < 10:
    if fuelType == "Petrol":
        print("The cheapest price of petrol today is:"), min(list1)
        print("")
        print ("This can be found at the")position in list1("petrol         station")
        print("The average price of petrol at all the stations today     is:"),avgPetrol 
        print("Just in case you were intersted, the average price of d     diesel today is:"),avgDiesel
Djizeus
  • 4,161
  • 1
  • 24
  • 42
M.C
  • 11
  • 1
  • 1
  • 4

3 Answers3

1

Include the variable in the print() function, e.g.:

value = 12
print('My value:', value)

You have your values outside the print() function like print('My value:'), value.

bastelflp
  • 9,362
  • 7
  • 32
  • 67
0
bpPetrol = 099.9
bpDiesel = 100.9

shellPetrol = 102.9
shellDiesel = 103.9

texacoPetrol = 100.9
texacoDiesel = 101.9

gulfPetrol = 098.9
gulfDiesel = 102.9

tescoPetrol = 100.9
tescoDiesel = 102.9

namelist=['bp', 'shell', 'texaco', 'gulf', 'tesco']
list1= [bpPetrol, shellPetrol, texacoPetrol, gulfPetrol, tescoPetrol]
list2= [bpDiesel, shellDiesel, texacoDiesel, gulfDiesel, tescoDiesel]

fuelType="Petrol"

if fuelType == "Petrol":
    minPetrolPrice=min(list1)
    minPetrolPriceIndex=list1.index(min(list1))
    minPetrolPriceName=namelist[minPetrolPriceIndex]
    print("The cheapest price of petrol today is: ", minPetrolPrice)
    print("")

    print("This can be found at the position ", minPetrolPriceIndex, "in list1 and petrol station name is ", minPetrolPriceName)
    print("The average price of petrol at all the stations today is:", (sum(list1)/float(len(list1))))
    print("Just in case you were intersted, the average price of d diesel today is:", (sum(list2)/float(len(list2))))

Above program displays the minimum petrol price, station name and index in the list along with averages

A better approach would be to use dictionaries having names mapped to prices

riteshtch
  • 8,629
  • 4
  • 25
  • 38
0

If what you want is to display the name of the cheapest brand, you can do so by storing the names separately or even by inspecting the globals() or locals() output, but it will be a lot easier if you store dictionaries (or objects, or some structured data) in the lists.

For example:

petrol_prices = [{
  'brand': 'BP',
  'price': 099.9,
},{
  'brand': 'Shell',
  'price': 102.9,
},
# etc...
]

Then you can use the key argument of the min function to work on these lists:

smallest = min(petrol_prices, key=lambda x: x['price'])
print("Cheapest brand: ", smallest['brand'], ", price: ", smallest['price'])

If you need more info on lambda functions, you can check this question. You can also give a regular function to the key argument if you prefer.

Community
  • 1
  • 1
Djizeus
  • 4,161
  • 1
  • 24
  • 42