程序在def计算1上返回语法错误

时间:2013-12-09 11:50:55

标签: python python-2.7 syntax-error function

#ask the user for how many books they would like
def books_needed():
    number_books = int(raw_input("How many books would you like?:"))
    if number_books <0 or number_books >80:
        number_books = raw_input(int("Number of books cannot be less than 0 or greater than 80. Enter another number:"

#calculates the cost of the books
def calculation1(number_books):
    cost_books = number_books * float(15.99)
    return cost_books

#selects the correct discount percentage to apply
def calculation2(number_books):
    if number_books <= 51 and number_books >= 81:
        discount = int(10)
    elif number_books <=11 and number_books >= 50:
        discount = float(7.5)
    elif number_books <= 6 and number_books >= 10:
        discount = int(5)
    elif number_books <= 1 and number_books >= 5:
        discount = int(1)
    return discount

#works out the cost of books divided by 100
def calculation3(discount,cost_books):
    total = cost_books/100
    return total

#multiplies the number by the correct discount
def calculation4(total,discount):
    full_discount = total * discount
    return full_discount

#calculates the total cost of the books including discount
def calculation5(full_discount):
    final_cost = cost_books - full_discount
    return final_cost

#displays the result
def display_results(final_cost):
    print
    print"Your final price is £", final_cost

#main program
books_needed()
cost_books = calculation1(number_books)
discount = calculate2(number_books)
total = calculation3(discount,cost_books)
full_discount = calculation4(total,discount)
final_cost = calculation5(full_discount)
display_results(final_cost)

准确地说,对def的计算1发生错误。如果需要,可以复制确切的错误。据我所知,其余程序还可以。如果出现其他任何问题,请告诉我。感谢

3 个答案:

答案 0 :(得分:4)

您忘记了上一行的一些右括号:

number_books = raw_input(int("Number of books cannot be less than 0 or greater than 80. Enter another number:"
#   two opening parens 1^  2^        but at the end here ---------------------------------- no closing parens ^

您需要在那里添加两个 )个字符,以关闭int()raw_input()来电:

number_books = raw_input(int("Number of books cannot be less than 0 or greater than 80. Enter another number:"))

然而,这仍然是错误的,因为"Number .."字符串不能转换为整数。您可能希望将两个调用反转为:

number_books = int(raw_input("Number of books cannot be less than 0 or greater than 80. Enter another number:"))

raw_input()(用户输入)的返回值转换为整数。

其他说明:

  • 您无需将数字文字转换为float()int()

    cost_books = number_books * 15.99  # 15.99 is a float already
    
    discount = 10    # 10 is an integer already
    
    discount = 7.5   # 7.5 is a float already
    
  • 您的books_needed()函数不会返回任何内容。从函数返回number_books以便稍后使用。在您拥有有效数字之前,请使用循环继续询问数字:

    def books_needed():
        while True:
            try:
                number_books = int(raw_input("How many books would you like?:"))
                if 1 <= number_books <= 80:
                    return number_books
            except ValueError:
                # not a number, at all
                pass
            print "Number of books must be between 1 and 80."
    

    然后在最后使用:

    number_books = books_needed()
    

答案 1 :(得分:0)

raw_input之前的def上没有右括号。这就是造成你问题的原因。

最重要的是,您有raw_inputint转置。前者获取字符串,后者将其转换为整数。因此它应该是:

int (raw_input( ...

答案 2 :(得分:0)

你不应该在行中使用raw_input:number_books = int(raw_input(“你想要多少本书?:”))

相反你应该只使用输入:number_books = input(“你想要多少本书?:”)