1

私のコードは、= 部分に「<= 80」と書かれている行でエラーを返しています。何故ですか?どうすれば修正できますか?

#Procedure to find number of books required
def number_books():
    number_books = int(raw_input("Enter number of books you want to order: "))
    price = float(15.99)
    running_total = number_books * price
    return number_books,price

#Procedure to work out discount
def discount(number_books):
    if number_books >= 51 and <= 80:
        discount = running_total / 100 * 10
    elif number_books >= 11 and <=50:
        discount = running_total / 100 * 7.5
    elif number_books >= 6 and <=10:
        discount = running_total / 100 * 5
    elif number_books >=1 and <=5:
        discount = running_total / 100 * 1
    else print "Max number of books available to order is 80. Please re enter number: "        
        return discount

#Calculating final price
def calculation(running_total,discount):
    final_price = running_total - discount

#Display results
def display(final_price)
print "Your order of", number_books, "copies of Computing science for beginners will cost £", final_price 

#Main program
number_books()
discount(number_books)
calculation(running_total,discount)
display(final_price)

どんな助けでも大歓迎です

4

2 に答える 2

6

これは無効です:

if number_books >= 51 and <= 80

試す:

if number_books >= 51 and number_books <= 80

その他のすべての出現と同じ

または、nneonneoが言及しているように、

if 51 <= number_books <= 80

また、最後に割引を正しい方法で返す必要があります (これは、この問題が解決されたときに発生する別の問題です)。

そう、

def discount(number_books):

    if 51 <= number_books <= 80:
        discount = running_total / 100 * 10
    elif 11 <= number_books <= 50: 
        discount = running_total / 100 * 7.5
    elif 6 <= number_books <= 10: 
        discount = running_total / 100 * 5
    elif 1 <= number_books <= 5:
        discount = running_total / 100 * 1

    return discount


def number_books():
    num_books = int(raw_input("Enter number of books you want to order: "))
    if numb_books <= 0 or num_books > 80:
        print "Max number of books available to order is 80, and minimum is 1. Please re enter number: "        
        number_books()

    price = float(15.99)
    running_total = num_books * price
    return number_books,price
于 2013-09-30T17:52:04.823 に答える
6

範囲テストを行っている場合は、連鎖比較を使用できます。

if 51 <= number_books <= 80:

and構文エラーが発生する理由については、 (or or) 演算子の両側が完全な式でなければなりません。<= 80は完全な式ではないため、構文エラーが発生します。number_books >= 51 and number_books <= 80その構文エラーを修正するには、書く必要があります。

于 2013-09-30T17:52:39.587 に答える