1

My code is returning an error on the line where it says "<= 80" on the = part. Why is that? How can I fix it?

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

any help would be greatly appreciated

SuperBob
  • 15
  • 3

2 Answers2

6

This is invalid:

if number_books >= 51 and <= 80

Try:

if number_books >= 51 and number_books <= 80

The same with all the other occurances thereof

Or, as nneonneo mentions,

if 51 <= number_books <= 80

Also, you need to return discount the right way at the end (That would be another issue you would encounter once this issue is resolved).

So,

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
karthikr
  • 97,368
  • 26
  • 197
  • 188
6

If you're doing range testing, you can use a chained comparison:

if 51 <= number_books <= 80:

As to why you get a syntax error: both sides of an and (or or) operator must be full expressions. Since <= 80 is not a complete expression, you get a syntax error. You'd need to write number_books >= 51 and number_books <= 80 to fix that syntax error.

nneonneo
  • 171,345
  • 36
  • 312
  • 383