Python percentage calculator does not call exit()

Question:

I am trying to write a percentage calculator that asks for the number of subjects, marks in the specified number of subjects and computes the percentage. It works well, but does not exit on calling exit() after the user presses "n":

value = input("Do you want to calculate again (y/n):")
if value.lower == "y":
   percentage()  
elif value.lower == "n":       
   print("ok, sayonara") 
   exit() 

The complete code is:

def percentage():           
    numbers = [] 
    x = int(input('How many subjects would you like to find the percentage for:')) 
    for i in range(x):
        n = int(input('subject ' + str(i+1) + ': '))
        numbers.append(n)
    final = sum(numbers) / len(numbers)
    print("The percentage is",final,"%")
while True:
    try:
        percentage()
        value = input("Do you want to calculate again (y/n):")
        if value.lower == "y":
           percentage()  
        elif value.lower == "n":       
           print("ok, sayonara") 
           exit() 
    except:
       print("nOops! Error.  Try again...n")

here’s what happens:
problem_image

Asked By: SK-the-Learner

||

Answers:

The problem is you are not calling the lower function. Use value.lower().

Answered By: sloppypasta

value.lower does not call the function, it refers to the function itself. So you are asking is a function equal to a string in value.lower == "n".

You need to change it to value.lower() == "n" and same for "y".

You also don’t have a condition for if value.lower() is neither "y" or "n".

Answered By: EDD

There are two bugs in your code:

if value.lower == "y":

Here you are comparing the function object value.lower with the string "y", they will always be different. You probably meant:

if value.lower() == "y":

And the same for == "n".

Fixing this does not fix the whole code because except: catches all interrupts including the exit() interrupt, so you need need to except ValueError: instead to allow the exit() interrupt to finish your program.

Here is the fixed code:

def percentage():           
    numbers = [] 
    x = int(input('How many subjects would you like to find the percentage for:')) 
    for i in range(x):
        n = int(input('subject ' + str(i+1) + ': '))
        numbers.append(n)
    final = sum(numbers) / len(numbers)
    print("The percentage is",final,"%")
    
while True:
    try:
        percentage()
        value = input("Do you want to calculate again (y/n):")
        if value.lower() == "y":
           percentage()  
        elif value.lower() == "n":       
           print("ok, sayonara") 
           exit() 
    except ValueError:
       print("nOops! Error.  Try again...n")
Answered By: Caridorc

The problem is the bare Except statement. Under the hood, exit() raises a SystemExit Exception that is being handled by your try except. This also makes it impossible to terminate the program with ctrl c and ctrl d, since these also raise an exception (KeyboardInterrupt and EOFError, respectively).

Being very straight forward, its good practice to never write Except in any code without specifying the type of exception, it is extremely prone to bugs

Answered By: PedroTurik
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.