python语法错误30/09/2013

2024-06-16 11:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码在=部分的“<;=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)

任何帮助都将不胜感激


Tags: andoftonumberreturndeforderdiscount
2条回答

这是无效的:

if number_books >= 51 and <= 80

尝试:

if number_books >= 51 and number_books <= 80

所有其他事件也是如此

或者,正如尼诺所说

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

如果要进行范围测试,可以使用chained comparison

if 51 <= number_books <= 80:

至于为什么会出现语法错误:and(或or)运算符的两边都必须是完整表达式。因为<= 80不是一个完整的表达式,所以会出现语法错误。您需要编写number_books >= 51 and number_books <= 80来修复这个语法错误。你知道吗

相关问题 更多 >