1

Recently I am creating a function:

def TaxiFare(the_number_of_km):
    if the_number_of_km >= 2:
        return 24
    elif the_number_of_km > 2:
        return 24 + int((the_number_of_km - 2)/0.2) * 1.7
    else:
        return None
        print('Something goes wrong !')
   
TaxiFare("Wrong")

I want to return None and print 'Something goes wrong !' when I input non-numerical values in the argument. However, it turns out:

TypeError: '>=' not supported between instances of 'str' and 'int'

How can I fix it?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
aukk123
  • 311
  • 3
  • 12
  • Learn about [exception handling](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) – Tomerikoo Nov 10 '20 at 09:11
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Nov 10 '20 at 09:11

3 Answers3

2

Try this:

def TaxiFare(the_number_of_km):
  try: 
    if the_number_of_km >= 2:
      return 24
    elif the_number_of_km < 2:
      return 24 + int((the_number_of_km - 2)/0.2) * 1.7
  except TypeError:
      print('something goes wrong !')
      return None
   

print(TaxiFare(3.7))

You can use try: except: to see if it have a error or not.

and did you mean this -> elif the_number_of_km < 2: instead of this -> elif the_number_of_km > 2:
because you are doing the statement 2 times. >= 2 and > 2

ppwater
  • 2,315
  • 4
  • 15
  • 29
  • Yes, it runs perfectly. However, I tried to pass a Boolean `True`, it turns out 24. Does it means that because the first statement is true, so it returns 24? thank you! – aukk123 Nov 10 '20 at 09:23
  • @aukk123 when I tried to pass `True` then It prints none how did you try to pass it? – ppwater Nov 10 '20 at 10:46
  • @aukk123 oh, I fixed something and I understand the reason. in python, int(True) is 1. – ppwater Nov 10 '20 at 10:53
0

All you need to do is to make sure the argument of the function is an integer, you can have another if statement checking if its an integer or string by using type() before executing your code.

def TaxiFare(the_number_of_km):
if type(the_number_of_km) == str:
    print('Something goes wrong !')
else:
    if the_number_of_km >= 2:
        return 24
    elif the_number_of_km > 2:
        return 24 + int((the_number_of_km - 2) / 0.2) * 1.7

TaxiFare("Wrong")
  • What about returning none? – ppwater Nov 10 '20 at 09:03
  • 1
    What if I only accept the_number_of_km is numeric value (either float or int)? For example, I tried to run your code and entered TaxiFare(True) which is a Boolean, and it return none and cannot print. Many thanks! – aukk123 Nov 10 '20 at 09:04
0

Do type checking at first.

def TaxiFare(the_number_of_km):
    if not isinstance(the_number_of_km, int):
        return None
        print('Something goes wrong !')
    elif the_number_of_km >= 2:
        return 24
    elif the_number_of_km > 2:
        return 24 + int((the_number_of_km - 2)/0.2) * 1.7
  • How about printing Something goes wrong ! this code doesn't do this – ppwater Nov 10 '20 at 09:09
  • 1
    Yes, I run perfectly when I input an int. However, how can I do if I also want to input float? For example I input 3.7 in the argument and it returns none. How can I fix this problem? Many thanks! – aukk123 Nov 10 '20 at 09:09
  • @ppwater, I changed the order and moved print(Something goes wrong !) above return None, and it can print – aukk123 Nov 10 '20 at 09:10
  • @aukk123 Yes, I did that too, but just wanted to improve this answer. (it should work when copied and pasted.) – ppwater Nov 10 '20 at 09:12