-2

I have two running counts in this code, and when I hit 'stop' to get a summary, the entries that should populate my running count are returning 0. It was working and then it wasn't, and I have no idea why.

print ('Property Type   Taxes')

n = str() #Property Type
m = float() #Amount of taxes

totalC = float(0.0) #total taxes of Commercial property
totalR = float(0.0) #total taxes of Residential property


while True:
    n = input()
    if n == 'stop':
        print ('Sum of Taxes' '   ' 'Average of Taxes')
        print ('Residential Properties' '   ', totalR)
        print ('Commercial Properties' '   ', totalC)
    m = float(input())
    print (n, '      ', m)
    if n == ('Commercial' or 'commercial'):
        totalC += m
    elif n == ('Residential' or 'residential'):
        totalR += m

There's only so much teaching my professor has actually done and the textbook we're using is extremely limited. I could not understand the variety of 'while' conditions, so I had while n != 'stop' as the condition and then I changed it to while True after seeing another thread. It worked after this and then stopped working after a few tries without having changed anything else.

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
squall1214
  • 11
  • 3
  • You should format your code properly. Particularly in Python, indentation is **everything** and so proper formatting is crucial. – TomServo Apr 17 '23 at 21:34
  • I'd suggest printing something if `n` isn't recognised, it helps to ensure that your code is doing what you expect – Sam Mason Apr 17 '23 at 21:40

1 Answers1

0

n == ('Commercial' or 'commercial') is checking if n is equal to the result of the boolean expression ('Commercial' or 'commercial'). Boolean expressions evaluate to the last operand evaluated. Since or is used and 'Commercial' is true, this expression returns 'Commercial'.

If n is 'Commercial' this will do what you expect, but otherwise it will not.

You likely wanted:

n == 'Commercial' or n == 'commercial'

But even better would be to first lower case n and then compare it to commercial.

n.lower() == 'commercial'

You might also use in to check an input against a set of possibilities.

n in {'Commercial', 'commercial'}
Chris
  • 26,361
  • 5
  • 21
  • 42