0

I'm a student in his first semester of computer science, so I'm still new to this whole thing. I'm making a program that converts percentages (only 1-100) to fractions of the smallest denominator. The code for doing this works just fine, however, it refuses to continue to the "Retry?" Section at the bottom. When I run the code, it reaches the lowest fraction, but seemingly gets stuck in a loop, as when I interrupt it, it says "

Traceback (most recent call last):
  File "C:\Users\dnktn\AppData\Roaming\Python\Python38\site-packages\sympy\utilities\misc.py", line 542, in as_int
    return operator.index(n)
TypeError: 'float' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\dnktn\Desktop\PercentToFraction.py", line 9, in <module>
    while sympy.isprime(percent) == False:
  File "C:\Users\dnktn\AppData\Roaming\Python\Python38\site-packages\sympy\ntheory\primetest.py", line 557, in isprime
    n = as_int(n)
  File "C:\Users\dnktn\AppData\Roaming\Python\Python38\site-packages\sympy\utilities\misc.py", line 542, in as_int
    return operator.index(n)
KeyboardInterrupt

Here is my code:

import sympy

retry = 'y'
while retry == 'y':
    percent = float(input('Enter percentage (0-100): '))
    denominator = 100
    divisor = 1
    if percent >= 1 and percent <= 100:
        while sympy.isprime(percent) == False:
            while divisor <= percent:
                if (percent % divisor) == 0 and (denominator % divisor) == 0:
                    percent /= divisor
                    denominator /= divisor
                    print(int(percent), '/', int(denominator))
                divisor += 1
            divisor = 2
        print(int(percent), '/', int(denominator))
    else:
        print('Invalid input!')
    retry = input('Retry? (y/n) ')

I imported SymPy as that was the only way I could find to easily determine if a number was prime. Any help is appreciated.

Tom Dude
  • 1
  • 1
  • The argument to `sympy.isprime()` has to be an integer, `percent` is a float. – Barmar Apr 28 '22 at 04:17
  • Use `int()` instead of `float()` when getting the precentage. – Barmar Apr 28 '22 at 04:20
  • Use [Greatest Common Divisor](https://stackoverflow.com/questions/11175131/code-for-greatest-common-divisor-in-python) to convert a fraction to simplest form. – Barmar Apr 28 '22 at 04:23
  • I switched the input from float to int, that didn't change the outcome. It still gives me the same error. – Tom Dude Apr 28 '22 at 05:08
  • There's almost certainly other logic and math problems. – Barmar Apr 28 '22 at 13:37
  • If you can use libraries, you could just use the `Fraction` class. It will automatically reduce to lowest terms. – Barmar Apr 28 '22 at 13:38

1 Answers1

0

I completely overhauled the code to use the gcd function from the math library. This is much simpler than what I started with.

The code:

from math import gcd
retry = 'y'
while retry == 'y':
    percent = int(input('Enter percentage (1-100): '))
    denominator = 100
    if percent >= 1 and percent <= 100:
        print(int(percent / gcd(percent, denominator)), '/', int(denominator / gcd(percent, denominator)))
    else:
        print('Invalid Input!')
    retry = input('Retry? (y/n) ')
print('Goodbye!')
Tom Dude
  • 1
  • 1