if else语句

时间:2016-02-29 04:55:36

标签: python python-3.x

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

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

错误如下所示:

  

语法错误:else sale_amount&lt; lowest_sale ::,line   58,pos 24

我看不出问题是什么,任何人都可以帮我找出错误的来源。感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

你应该首先学习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,如下所示:

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等等。

相关问题