【问题标题】:python 3.5.1 syntax error on an if else statement [duplicate]if else语句上的python 3.5.1语法错误[重复]
【发布时间】:2016-02-29 04:55:36
【问题描述】:

我正在做一个家庭作业,它看起来像这样:

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

错误如下所示:

语法错误:否则 sale_amount

我看不出问题出在哪里,谁能帮我找出错误的来源。感谢您的帮助。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    你应该先学习python的基本语法。看看python中的if and elif

    elif 部分可以有零个或多个,else 部分是可选的。 关键字elifelse if 的缩写,有助于避免 缩进过多。

    所以在第一个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,则应在条件前添加 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 等等。

    【讨论】:

    • 感谢您的建议。我认为代码中会有更多错误,我询问的错误只是调试器中出现的第一个错误,我终其一生都无法弄清楚它为什么不起作用。我刚刚完成了整个任务,并且已经调试了所有内容并正常工作。感谢您的宝贵时间。
    【解决方案2】:

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

    【讨论】: