Home > Software design >  Trying to write python code to calculate taxi fare where £1 is charged for each mile but £1 is deduc
Trying to write python code to calculate taxi fare where £1 is charged for each mile but £1 is deduc

Time:10-27

def main():
    print("welcome to the taxi fee game")
    miles = int(input("please enter your distance in miles: ")
    print("your fee is",  1*miles)

CodePudding user response:

The rule is easy

  • the distance is lower than 10 : fare == miles
  • else fare == miles-1
miles = int(input("please enter your distance in miles: "))
fare = miles if miles < 10 else miles - 1
print("your fee is", fare)

CodePudding user response:

If you want to do this in a 1 line snippet with the print statement you can do something like this for an inline conditional.

print("your fee is", 1*miles if miles <= 10 else (1*miles) -1)

So for example if:

miles = 10
print("your fee is", 1*miles if miles <= 10 else (1*miles) -1)
# your fee is 10


miles = 12
print("your fee is", 1*miles if miles <= 10 else (1*miles) -1)
# your fee is 11

So your changed code becomes:

def main():
    print("welcome to the taxi fee game")
    miles = int(input("please enter your distance in miles: ")
    print("your fee is", 1*miles if miles <= 10 else (1*miles) -1)
  • Related