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.