Похожие чаты

Hello. I just want someone to check this code. is

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

5 ответов

30 просмотров

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?

ANDRIO- Автор вопроса
ANDRIO- Автор вопроса
Fayaz Khan
Why are you using the NameError there though?

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

ANDRIO
No. The lesson was exception handling. Thanks

... yes, the assert and raise keywords raise exceptions, which you can handle

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта