2016-02-29 6 views
1

я работаю на домашнее задание, это выглядит следующим образом:питон 3.5.1 синтаксическая ошибка на если другое заявление

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

Я не могу увидеть, что вопрос, может кто-нибудь помочь мне выяснить, где Т он исходит из ошибки. Спасибо за любую помощь.

ответ

2

Сначала вы должны изучить базовый синтаксис python. Взгляните на if and elif на python.

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»).

Смежные вопросы