hey y’all!
I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure.
I’m running into an infinite loop when the exceptions are raised in the convert() function.
I just don’t understand why. In both exceptions the “continue” statement after the print should restart the loop back the the beginning (at least that’s my logic)…but it’s not happening.
Why?
Any ideas / suggestions to improve my code are appreciated.
Where is my logic flawed?
import string
def main():
fraction=input("Fraction: ")
percentage=convert(fraction)
print(f"% {percentage}")
zz=gauge(percentage)
print(zz)
def convert(fraction):
while True:
try:
x,y=fraction.split("/")
x=int(x)
y=int(y)
if y==0:
raise ZeroDivisionError
elif x>y:
raise ValueError
except ValueError:
print("found ValueError")
continue
except ZeroDivisionError:
print("found Zero Division Error")
continue
else:
z=round((x/y)*100)
print(z)
return z
def gauge(percentage):
if percentage<=1:
return "E"
elif percentage >=99:
return "F"
else:
return f"{percentage}%"
if __name__=="__main__":
main()