sully sully - 29 days ago 10
Python Question

Calculate parking fee. '&' versus 'and'

I am trying to write a Python program that charges the parking fee for how many hours you have parked.

Everything works fine until the minutes exceed 300.

I have played with returns and every time I did that after the input I would get successful completion no output.

When I put in 600 minutes (10 hours) I get a fee of 40 dollars when it should be 30 dollars.

Here is my code:

import math
rate1 = 5
rate2 = 4
rate3 = 3


m = int(input('Please enter the number of minutes parked: '))

if m <= 60:
x = m/60
fee = math.ceil(x) * 5
print('Parking fee for',m,'minutes is $',fee)

elif m>60 & m<=300:
x = m/60
fee = math.ceil(x) * rate2
print('Parking fee for',m,'minutes is $',fee)

elif m>300:
x = m/60
fee = math.ceil(x) * rate3
print('Parking fee for',m,'minutes is $',fee)

else:
print('Invalid input')


output:

Please enter the number of minutes parked: 600
Parking fee for 600 minutes is $ 40

Process finished with exit code 0

Answer Source
if m > 60 & m <= 300:

should be:

if m > 60 and m <= 300:

or

if 60 < m <= 300:

& is the bit-wise AND operator, and is the logical AND operator (this is analogous to the difference between & and && in C, PHP, and Javascript).