Program is ignoring the last elif

import math

Type=str(input("what type of fuel does your car require? "))
amount=float(input("how much fuel have you put into your car in litres? "))
if Type==“petrol” or “Petrol”:
total=1.40amount
elif Type==“diesel” or “Diesel”:
total=1.55
amount
elif Type==“lpg” or “LPG” or “Lpg”:
total=0.95amount
given=float(input("how much money have you given? "))
change=given-total
print(“you are owed £”,change,“change”)
loyalty=str(input("do you have a loyalty card? "))
if loyalty==“yes” or “Yes”:
whole=math.floor(total)
full=math.floor(amount)
points=whole + full
if points>100:
points2=points
1.1
else:
points2=points
print(“you have gained”,points2,“points”);

elif:
print(“have a nice day”)

Please format your code per the instructions in this post: About the Python Help category

Your final elif is missing a condition. Perhaps you meant to use else? As it is, your code will result in a SyntaxError.

Also, most of the conditions in your if clauses always evaluate to True, so none of the elif or else clauses will ever run. For example

if Type=="petrol" or "Petrol":
    total=1.40*amount

There are two problems here: First, == has higher priority than or, so this will be interpeted as (Type=="petrol") or "Petrol"

Secondly, in python, a non-empty string (like “Petrol”) is considered to be True when used in a condition. Therefore, this code is equivalent to

if (Type=="petrol") or True:
    total=1.40*amount

which will of course always run and never enter any elif or else clauses.

You should do something like this instead:

if Type in ["petrol", "Petrol"]:
    total=1.40*amount

You should also consider adding some error handling, if for example the user enters “gasoline” or something else unexpected.

2 Likes

This is a very common “gotcha”.

Python code is very close to English, close enough that people think that:


if Type == "petrol" or "Petrol":

means the same as it would in English. But it doesn’t. It does not mean

if Type equals "petrol" or Type equals "Petrol" (with a capital P)

It means:

if (Type equals "petrol") makes a true value,

OR if ("Petrol") makes a true value

The problem here is that strings like “Petrol” are always true values (except for the empty string).

So the first test of your if...elif... chain is always true, and none of the elif statements will run.

You can see this for yourself:


if "Petrol":

    print("Petrol counts as a true value")

else:

    print("Petrol counts as a false value")

Or more compactly:


print(bool("Petrol"))  # prints True

How do you fix it?


if Type in ("petrol", "Petrol"):

    ...

elif Type in ("diesel", "Diesel", "DIESEL"):

    ...

Or even better:


Type = Type.lower()

if Type == "petrol":

    ...

elif Type == "diesel":

    ...

elif Type == "lpg":

    ...

2 Likes

You don’t need the strType’ here, because the input() function returns a string by default; the same goes for the other inputs, where a user is entering a string at said input.

Also, be mindful of your variable names – Type could be confusing; better to use the likes of fuel_type, in this instance.

@abessman has offered a nice way to do this, but you could also use:

if fuel_type == "petrol" or fuel_type == "Petrol":
2 Likes