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”:
elif Type==“diesel” or “Diesel”:
elif Type==“lpg” or “LPG” or “Lpg”:
given=float(input("how much money have you given? "))
print(“you are owed £”,change,“change”)
loyalty=str(input("do you have a loyalty card? "))
if loyalty==“yes” or “Yes”:
points=whole + full
print(“you have gained”,points2,“points”);
print(“have a nice day”)
Please format your code per the instructions in this post: About the Python Help category
elif is missing a condition. Perhaps you meant to use
else? As it is, your code will result in a
Also, most of the conditions in your
if clauses always evaluate to True, so none of the
else clauses will ever run. For example
if Type=="petrol" or "Petrol":
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:
which will of course always run and never enter any
You should do something like this instead:
if Type in ["petrol", "Petrol"]:
You should also consider adding some error handling, if for example the user enters “gasoline” or something else unexpected.
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)
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:
print("Petrol counts as a true value")
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":
You don’t need the
str ‘Type’ 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":