TL;DR: Use the int()
function to convert your input to a number
price = int(input('How much did your taxi ride cost?:'))
Long answer
In general, when something is not going as expected in your code, you should try to figure out what could be causing it based on the information your code provides you. One way to do that would be to try printing the if clause before the if
statement is evaluated! For example, you could try, right before the if
:
print(input < 45)
You say your if block gets ignored. That probably means its test is returning a falsy value! The way if
works is that it will execute its block if and only if whatever comes between if
and :
evaluates to a truthy value (truthiness varies per language, but True
/true
/YES
/etc—whichever your language uses—is definitely truthy).
For your specific case, you're asking "is price
strictly less than 45?", and your if statement is saying nah, so it doesn't execute. The reason is that the input()
function returns a string rather than a number, so comparing it to 45 means you're comparing text to a number. See this question to see how that works in Python:
CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
To solve your issue, convert your string result to a number (an integer, in this case) before comparing it with another number. You do that by calling the int()
function on the result of input()
, like:
price = int(input('How much did your taxi ride cost?:'))