0

I've just started to learn python and can't get this code to work properly.

def percentage():
    while True:
        try:
            x = int(input('First number:'))
            y = int(input('Second number:'))


            fraction = x/y
            percentage = fraction * 100
            final = "%d" % percentage

            return final

        except ZeroDivisionError:
            pass

        except ValueError:
            pass

if percentage() <= 1:
    print('E')

elif percentage() >= 99:
    print('F')

else:
    print(percentage(), "%")

The 'F' is printed only when I input x = 999 and y = 1000. Any other values just prompt the user again and again. The code should output 'E' when the percentage function returns <= 1, 'F' when it returns >= 99 and the actual percentage number when any other value is returned.

2
  • This fails with a TypeError as final a string that you then try to compare with 1. Commented Dec 30, 2022 at 19:12
  • 1
    Your function returns a str but then you try to compare it to a number. Instead, you probably want to just return percentage. After fixing that you will still not have the desired functionality because every time your code gets to a point where it reads percentage(), it will call the function again. Instead you want to save the result of calling percentage() in a variable by doing my_variable = percentage() and then comparing and printing just with my_variable Commented Dec 30, 2022 at 19:13

2 Answers 2

4

I am getting a type error as final is a string which you then try to compare with an integer. The issue that you are asking about, however, is due to 3 calls of percentage(). Instead call it once and assign the return value a variable then run your tests against the variable:

def percentage():
    while True:
        try:
            x = int(input('First number:'))
            y = int(input('Second number:'))
            return 100 * x / y
        except ZeroDivisionError:
            print('x')
            pass
        except ValueError:
            print('y')
            pass


p = percentage() 
if p <= 1:
    print('E')
elif p >= 99:
    print('F')
else:
    print(p, "%")
Sign up to request clarification or add additional context in comments.

Comments

0

I would restructure your program so that your percentage function returns the percentage as a float instead of a string, and deal with the string formatting in your print statements.

We should also set some variable equal to the output of your function using something like p = percentage(), and then use p in your conditionals so that we don't unintentionally run percentage() multiple times from within your conditionals.

def percentage():
    while True:
        try:
            x = int(input('First number:'))
            y = int(input('Second number:'))


            fraction = x/y
            percentage = fraction * 100
            # final = "%d" % percentage

            return percentage

        except ZeroDivisionError:
            pass

        except ValueError:
            pass

p = percentage()
if p < 1:
    print('E')

elif p >= 99:
    print('F')

else:
    print(f"{p:.2f}%")

Example program run:

First number:2
Second number:3
66.67%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.