2014-11-04 39 views
0

我越来越与功能相混淆,因为我刚才想使用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() 

的其他上第二行是作为错误提出来的,所以要在靠近顶部的位置标识。 任何帮助将不胜感激。 谢谢。

+0

检查您的缩进和'if else'子句。 – 2014-11-04 12:32:28

+0

这不会运行。缩进是不连贯的。 – khelwood 2014-11-04 12:34:44

+0

你有2个空的'if'块 – 2014-11-04 12:35:10

回答

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: 

应该给你更多你所期望的。 你也将与您获取输入它期待一个参数时,它应该返回一个它应该看起来更像这个问题:

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

你可能会想所有的奖金代码的缩进成一个函数并将getInput返回给它

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) 
相关问题