2016-02-29 64 views
1

我工作的家庭作业语法错误,它看起来像这样:蟒蛇3.5.1上如果else语句

def main(): 
    keep_going = 'y' 
    number_of_salespeople = 0 

    while keep_going == 'y' or keep_going == 'Y': 
     process_sales() 
     number_of_salespeople += 1 
     keep_going = input('Are there more salespeople? (enter y or Y for yes) ') 

    print(' ') 
    print('There were', number_of_salespeople, 'salespeople today.') 

def process_sales():   
    print(' ') 
    name = input('What is the salesperson\'s name? ') 

    first_sale_amount = float(input('What is', name, '\'s first sale amount? ')) 
    while 0 <= first_sale_amount <= 25000: 
     print('Error: that is not a valid sales amount. The amount must be greater than 0') 
     first_sale_amount = float(input('Please enter a correct sale amount: ')) 

    highest_sale = first_sale_amount 
    lowest_sale = first_sale_amount 
    average_sale = first_sale_amount 

    number_of_sales = float(input('How many sales did', name, 'make this month? ')) 

    for number in range(2, number_of_sales + 1): 
     sale_amount = float(input('Enter', name, '\'s time for sale #' + str(number) + ': ')) 
     while 0 <= sale_amount <= 25000: 
      print('Error: that is not a valid sales amount. The amount must be greater than 0') 
      sale_amount = float(input('Please enter a correct sale amount: ')) 

    if sale_amount > highest_sale: 
     highest_sale = sale_amount 

    else sale_amount < lowest_sale: 
     lowest_sale = sale_amount 

    total_sales += sale_amount 
    average_sale = (first_sale_amount + total_sales)/number_of_sales 

    print('The highest sale for', name, 'was', \ 
      format(highest_sale, ',.2f'), \ 
      sep='') 

    print('The lowest sale for', name, 'was', \ 
      format(lowest_sale, ',.2f'), \ 
      sep='') 

    print('The average sale for', name, 'was', \ 
      format(average_sale, ',.2f'), \ 
      sep='') 

main() 

我有错误是在if else语句向底部,

if sale_amount > highest_sale: 
    highest_sale = sale_amount 

else sale_amount < lowest_sale: 
    lowest_sale = sale_amount 

错误看起来是这样的:

Syntax Error: else sale_amount < lowest_sale:: , line 58, pos 24

我看不出是什么问题,谁能帮我找出其中t他的错误来自于。感谢您的帮助。

回答

2

你应该首先研究python的基本语法。看看python中的if and elif

There can be zero or more elif parts, and the else part is optional. The keyword elif is short for else if, and is useful to avoid excessive indentation.

所以之后的第一个if检查更多的条件,你需要的东西是这样的:

if cond1: 
    # do something 
elif cond2: 
    # do something else 
# more elif branches if needed, and finally else if there is something default 
else: 
    # do the default thing 

有更多的问题与您的代码。

1.input由于传递了多个参数而错误。您可以使用字符串的方法format像这样:

first_sale_amount = float(input("What is {}'s first sale amount?".format(name))) 

2.的条件while有错误的逻辑。如果你想检查输入的值是在0-25000以下的范围内,你应该把一个not的条件之前,像这样:

while not 0 <= first_sale_amount <= 25000: 
     print('Error: that is not a valid sales amount. The amount must be greater than 0') 
     first_sale_amount = float(input('Please enter a correct sale amount: ')) 

,可能3,4,5等。

+0

谢谢您的建议。我认为在代码中会出现更多的错误,我问到的错误只是第一个出现在调试器中的错误,我不能为了我的生活找出它为什么不起作用。我刚刚完成了整个任务,并且调试了一切并正常工作。谢谢你的时间。 – MauginRa

2

else不能有条件。更改为elif(“else if”)。