it correct use of exception handling? also did i do right putting if condition to check if x is greater than y or is there any way i can check that using try except?
https://cs50.harvard.edu/python/2022/psets/3/fuel/
while True:
try:
x, y = map(int, input('Fraction: ').split('/'))
perc = int(round((x/y)*100))
if x>y:
continue
except(ValueError, NameError, ZeroDivisionError):
pass
else:
if perc<=1:
print('E')
elif perc>=99:
print('F')
else:
print(f'{perc}%')
break
you can use the assert or raise keywords for the comparison, but i think it's fine
Why are you using the NameError there though?
No. The lesson was exception handling. Thanks
Oh. Yes i forgot. I used it in previous exercise. I used it just in case the input doesn't get stored in variable, but here I'm asking for input again so it's useless. Thanks
... yes, the assert and raise keywords raise exceptions, which you can handle
Обсуждают сегодня