-3

I am trying to make a code so that the amount of Books will decide on each statements' usage. So if I write 20, the expected answer should be 95, instead it's 100. That means elif is being ignored, and is only doing the if statement (other than the negative elif statement). How can I rewrite the code so that it doesn't ignore the Book > 10 and Book > 20 elif statements?

try:  
  book = raw_input('Enter amount of Books:\n')  
  Book = int(book)  
  red = Book - 10  

  if Book > 0:  
    print int(book) * 5  
  elif Book > 10:  
    print (red * 4.5) + (10 * 5)  
  elif Book < 0:  
    print ('Amount cannot be negative.')  
  elif Book > 20:  
    print (10 * 5) + (10 * 4.5) + (int(book) - 20 ) * 4  
  else:  
    print ('There has been a mistake. Please restart.')

except:  
  print ('Error.')
Xenyal
  • 2,136
  • 2
  • 24
  • 51
Bared
  • 1
  • 2
  • Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – litelite Sep 20 '17 at 16:55
  • Unindented python code is nonsense. We need your code with the indentation. – khelwood Sep 20 '17 at 16:57
  • Fixed. Sorry, I'm new. – Bared Sep 20 '17 at 16:58
  • Will a number less than zero ever be greater than 10? – kaza Sep 20 '17 at 16:58
  • 1
    If `Book` is not greater than zero, then it is *utterly impossible* for it to be greater than ten, or greater than twenty. You are basically checking your options in the wrong order. – jasonharper Sep 20 '17 at 17:00
  • Are you sure you understand the purpose of an "___else___ _if_" ("elif" for short) statement? – Aran-Fey Sep 20 '17 at 17:01
  • So I have to move the Book < 0 part to the top? – Bared Sep 20 '17 at 17:02
  • @Bared Not necessarily, the `Book < 0` case is an exclusive case that isn't covered by your `elif` statements. However, consider `n = 25`. Given `n`, `n` satisfies `n > 20`, `n > 10`, and `n > 0` simultaneously. Thus, you need to ensure that the most accurate case is fulfilled first, that being `n > 20`. – Xenyal Sep 20 '17 at 17:06

4 Answers4

3

The elif statement means else if, that means the statement is evaluated only if the first one is wrong, in your case if book>0 is true and the result is 100: others conditions are not evaluated.

You just have to fix your condition order, also pay attention to your indentation :

try:  
    book = raw_input('Enter amount of Books:\n')  
    Book = int(book)  
    red = Book - 10  

    if Book > 20:
        print (10 * 5) + (10 * 4.5) + (int(book) - 20 ) * 4
    elif Book > 10:  
        print (red * 4.5) + (10 * 5)  
    elif Book > 0:
       print int(book) * 5   
    elif Book < 0:  
       print ('Amount cannot be negative.')  
    else:  
       print ('There has been a mistake. Please restart.')

except:  
    print ('Error.')
Coding thermodynamist
  • 1,340
  • 1
  • 10
  • 18
1

Your first if statement will always catch the cases where n > 10 and n > 20. Thus, your conditions should begin with a base case; one that won't be so easy to fulfill such as:

if Book < 0:  
  print ('Amount cannot be negative.')

Continue onwards with the cases that will be caught first because the order of conditions will matter in your case since they aren't mutually exclusive, that being n > 20. And thus the code would look something like the following:

if Book < 0:  
  print ('Amount cannot be negative.')
elif Book > 20:  
  print (10 * 5) + (10 * 4.5) + (int(book) - 20 ) * 4
elif Book > 10:  
  print (red * 4.5) + (10 * 5)
elif Book > 0:  
  print int(book) * 5
else:
  print ('There has been a mistake. Please restart.')
Xenyal
  • 2,136
  • 2
  • 24
  • 51
0
try:  
    book = raw_input('Enter amount of Books:\n')  
    Book = int(book)  
    red = Book - 10 
    if Book < 0:
        print ('Amount cannot be negative.')
    else:
        if Book > 20:  
            print 50 + 45 + (Book - 20) * 4
        elif Book > 10:
            print (red * 4.5) + (10 * 5)
        elif Book > 0:  
            print Book * 5  
        else:  
            print ('There has been a mistake. Please restart.')
except:  
    print ('Error.')
Anton Hulikau
  • 336
  • 1
  • 3
  • 7
0

Try to eliminate edge cases wherever possible.

Putting Book > 10 covers all values greater than 10. Range: [10,)
So no importance of putting Book > 20 after that.

Put your statements according to the priority.

if Book > 20:
    print (10 * 5) + (10 * 4.5) + (int(book) - 20 ) * 4
elif Book > 10:  
    print (red * 4.5) + (10 * 5)  
elif Book > 0:
   print int(book) * 5   
elif Book < 0:  
   print ('Amount cannot be negative.')  
else:  
   print ('There has been a mistake. Please restart.')
bhansa
  • 7,282
  • 3
  • 30
  • 55