2013-09-30 1 views
1

코드에서 "< = 80"부분에 오류가 반환됩니다. 왜 그런가요? 어떻게 해결할 수 있습니까?python 구문 오류 30/09/2013

if number_books >= 51 and <= 80 

시도 :

if number_books >= 51 and number_books <= 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) 

어떤 도움을 크게 이것은 잘못된

+1

전체 오류를 게시하십시오. – thegrinner

+0

@thegrinner : 문법은 아주 분명히 틀리며 일반적인 초보자도 실수입니다. –

+2

@MartijnPieters 나도 알아, 난 그냥 미래에 더 많은 질문이 있으면 것들을 천천히하지 않을 전체 오류를 게시하는 습관에 OP를하고 싶습니다. – thegrinner

답변

6

을 감상 할 수있다 , a 언급 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 
+4

더 나은,'51 <= number_books <= 80' – nneonneo

6

, 당신이 사용할 수있는 chained comparison :

if 51 <= number_books <= 80: 

당신이 구문 오류 얻을 이유에 : (AN and의 양쪽 또는 or) 연산자는 완전한 표현식이어야합니다. <= 80이 완전한 표현식이 아니기 때문에 구문 오류가 발생합니다. 구문 오류를 수정하려면 number_books >= 51 and number_books <= 80을 작성해야합니다.

+0

만약 당신이 문서에 링크 할 수도 있습니다; 이 기능을 '연쇄 비교'라고합니다. 그러나 OP가 어디에서 잘못 이해되었는지는 설명하지 않았습니다. –

+0

감사합니다. 문서 링크가 추가되었습니다. – nneonneo

+0

IMO가 현재 질문/의심에 대한 적절한 대답이 아니기 때문에 그가 왜 오류를 얻고 있는지 더 자세히 설명하십시오. – KurzedMetal