0

Hi everyone .

I have two problems here the first one: why does not keyerror’s except work here despite of typing items which is not exsit the second when I click control d nothing happens and what the purpose of eof here? https://www.sololearn.com/en/compiler-playground/cQk7Y03Eolnq

1st Jul 2024, 4:29 PM
Yusof
Yusof - avatar
4 Réponses
+ 1
KeyError exception is not raised (triggered) because there is that `if` conditional at line 20 which prevents the attempt to refer a dictionary item from <menu> dictionary at line 21. The `if` conditional at line 20 checks whether there is an item having <order> as its key in <menu> dictionary. If such item exists, its value will be added into <cost>. If you omit the check at line 20 and just go ahead try to accumulate the cost, you will have KeyError raised when <order> contains invalid key. Where did you use "control d"? such key combination only works in real console interface. It won't work on SoloLearn.
1st Jul 2024, 5:47 PM
Ipang
+ 1
Hello Yusof Sorry for late reply, I didn't get the mention, I'm accessing SoloLearn from the web :) Look here... if order in menu: cost += menu[order] The `if` statement checks whether <menu> dictionary has any item whose key equals <order> variable value. When <menu> dictionary has such item, the item's value will be added into <cost> variable. When no match was found in <menu>, there will be no attempt to refer `menu[order]`, thanks to the check performed by `if` statement. Consequently, KeyError exception will not be raised. * KeyError exception is raised when our code tries to refer a non-existing dictionary item (no item key matches the given key).
9th Jul 2024, 8:30 PM
Ipang
0
ThaksIpang actually I have tried the code on sololearn. Do you mean if statement prevents implementing key error's except.
2nd Jul 2024, 8:17 PM
Yusof
Yusof - avatar
0
Thanks no matters if you were let I appreciate
11th Jul 2024, 12:45 PM
Yusof
Yusof - avatar