-3

Sorry if I'm making a simple mistake but I really can't figure this out. I need to create a change machine for my computer science class and I know I've used a less than or equal to statement, but for some reason its not working now.

Amount= float(input("What is the dollar amount?"))

Change = 0
q = 0
d = 0
n = 0
p = 0

while Amount > 0:
  if Amount >= .25:
    Q = Amount - .25
    q += 1
  elif Amount > .10 and <= .25:
    D = Amount - .10
    d += 1
  elif Amount > .05 and <= .10:
    N = Amount - .05
    n += 1
  elif Amount < .05:
    P = Amount - .01
    p += 1

print q
print d
print n
print p

Error:

    line 18
          elif Amount > .10 and <= .25
                         ^
    SyntaxError: invalid syntax
Vasfed
  • 18,013
  • 10
  • 47
  • 53

3 Answers3

0

Try this one:

elif Amount > .10 and Amount <= .25

Also it is good to say, for clear programming, you should create variables with lowercase.

Edit: zondo (in comments) is right; it would be better to do:

elif .10 < Amount <= .25
zondo
  • 19,901
  • 8
  • 44
  • 83
stuck
  • 1,477
  • 2
  • 14
  • 30
0

You can use chain comparisons in python:

elif .10 < Amount <= .25:

But also you're not modifying the Amount inside loop, so it's going to be running very long.

Vasfed
  • 18,013
  • 10
  • 47
  • 53
0

You don't even need the and because if the condition before (>= 0.25, for example) isn't true, then you don't need to check it again (< 0.25, in that case) because you know it is false.

Your while loop will never stop because you never decrease the value of Amount.

So, change like so

while Amount > 0:
  if Amount >= .25:
    Amount -= .25
    q += 1
  elif Amount >= .10:
    Amount -= .10
    d += 1
  elif Amount >= .05:
    Amount -= .05
    n += 1
  else:
    Amount -= .01
    p += 1

There will also be no change since the p value will consume the remaining amount.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245