-1

Im learning if statements. I wrote this code and ran it. It seems that lines 2 and 3 are ignored. When I enter a number lower than 45 it dosent prompt lines 2 and 3. I hope you understand what I mean.

price = input('How much did your taxi ride cost?:')
if price < 45:
  print('Processing')
if price > 45:
      response = ('Your taxi cost over $45 you will be charged a $5.00 fee')
      print(response)
      response = input('Would you like to proceed:')
      if response == 'yes': 
        print('Processing...')
if response == 'no':
    print('!Error!')
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
  • 2
    What Python version are you on? Python 2 `input` would have given you an int if you typed in something like `12`, and Python 3 would have thrown a TypeError on the comparison. You're not telling us the whole story. – user2357112 Mar 06 '18 at 22:16
  • Protip 2: use if / else / elif – max Mar 06 '18 at 22:17
  • Great read: [How to debug small programs (#4)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Patrick Artner Mar 06 '18 at 22:36

2 Answers2

1

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?:'))
sleighty
  • 895
  • 9
  • 29
  • also needs to handle exceptions. the user may not enter a number. – ColonelFazackerley Mar 06 '18 at 23:15
  • @ColonelFazackerley you’re right. I assume OP isn’t too worried about valid input as he’s learning conditional statements, so didn’t want to clutter the answer. Do you have a suggestion on how I can edit it? – sleighty Mar 06 '18 at 23:26
  • @Bruno_Ely I think your answer is good as it stands. Just thought that deserved a mention in the comments. – ColonelFazackerley Mar 07 '18 at 06:35
0

Use if and elif statements.

the correct usage for if statements is

if

then:

elif you can have as many elif as you like ...

then last, if nothing matches.

else

you can use either int or 'float` depending on the type of number to take in the user input, otherwise it will return a string.

int is a whole number ie 1, 2, 3 and float can take in a decimal ie 1.5, 2.3, 3.5

for this answer i have used float.

price = float(input('How much did your taxi ride cost?:'))
if price < 45:
  print('Processing')
elif price > 45:
    response = ('Your taxi cost over $45 you will be charged a $5.00 fee')
    print(response)
    response = input('Would you like to proceed:')
    if response == 'yes': 
        print('Processing...')
    elif response == 'no':
        print('!Error!')
johnashu
  • 2,167
  • 4
  • 19
  • 44