想知道为什么我一直收到错误

时间:2014-11-04 12:30:20

标签: python

我对功能感到困惑,因为我刚尝试使用if和else 功能,我不能让他们工作......

def instructions():
   print ("Hello, and welcome to the programme")

def getInput(b):
        b=int(input("Enter how many bags of Java coffe you have purchased:  "))

if  b <= 25:
    bonus = b + 0:

if  b >= 25:
     else:
         b <= 49:
        bonus = b * 0.05:

if  b <= 50:
else:
    b >= 99:
        bonus = b * 0.1:


if  b >= 100:
    bonus = b * 0.2:








...
instructions()
...
print("")
...
getInput()

第二行的else会出现错误,因此要识别顶部附近的b。 任何帮助将不胜感激。 感谢。

2 个答案:

答案 0 :(得分:0)

让我帮你解决一下

if  b <= 25:
    bonus = b + 0:

elif  b >= 25 and b <= 49:
    bonus = b * 0.05:

elif  b >= 50 and b <= 99:
    bonus = b * 0.1:
else:
    bonus = b * 0.2:

应该给你更多你期望的东西。 你的get输入也会遇到问题,它应该返回一个参数,它应该看起来更像这样:

def getInput():
    return int(input("Enter how many bags of Java coffe you have purchased:  "))

您可能希望将所有红利代码缩进到自己的函数中并将getInput的返回值传递给它

答案 1 :(得分:0)

您的代码缩进不正确 - 这在python中很重要;它是语法的一部分。

逻辑也可以做一些清理工作;

def instructions():
  print ("Hello, and welcome to the programme")


def getInput(b):
  b=int(input("Enter how many bags of Java coffe you have purchased:  "))

  bonus = 0:

  if 25 < b <= 50:
    bonus = b * 0.05

  elif b <= 99:
    bonus = b * 0.1

  elif b >= 100:
    bonus = b * 0.2:

  return b, bonus

...
instructions()
...
bagsBought, bonus = getInput()
print "You bought %i, and got %i bonus bags!" % (bagsBounght, bonus)