Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我在一个非常简单的小于或等于if语句中遇到语法错误_Python_If Statement_Syntax Error - Fatal编程技术网

Python 我在一个非常简单的小于或等于if语句中遇到语法错误

Python 我在一个非常简单的小于或等于if语句中遇到语法错误,python,if-statement,syntax-error,Python,If Statement,Syntax Error,对不起,如果我犯了一个简单的错误,但我真的无法理解。 我需要为我的计算机科学课程创建一个更改机器,我知道我使用了小于或等于的语句,但由于某些原因,它现在不起作用 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 eli

对不起,如果我犯了一个简单的错误,但我真的无法理解。 我需要为我的计算机科学课程创建一个更改机器,我知道我使用了小于或等于的语句,但由于某些原因,它现在不起作用

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
错误:

    line 18
          elif Amount > .10 and <= .25
                         ^
    SyntaxError: invalid syntax
试试这个:

elif Amount > .10 and Amount <= .25
另外,为了清晰地编程,最好使用小写字母创建变量

编辑:评论中的zondo是正确的;最好是:

elif .10 < Amount <= .25

可以在python中使用链比较:

elif .10 < Amount <= .25:
但是,您也没有修改循环中的数量,因此它将运行很长时间。

您甚至不需要,因为如果>=0.25之前的条件不正确,那么您不需要再次检查它<0.25,在这种情况下,因为您知道它是错误的

您的while循环永远不会停止,因为您永远不会减少Amount的值

所以,像这样改变

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
由于p值将消耗剩余的金额,因此也不会有任何更改