r/pythonhelp • u/Mattbown7 • May 09 '22
How is "B, C, D" printed?
class B(Exception):
____pass
class C(B):
____pass
class D(C):
____pass
for cls in [B, C, D]:
____try:
________raise cls()
____except D: print("D")
____except C: print("C")
____except B: print("B")
The underscores are spaces.
2
u/Goobyalus May 09 '22
OP's code formatted:
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D: print("D")
except C: print("C")
except B: print("B")Output of OP's code:
B
C
D1
1
u/skellious May 09 '22
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D: print("D")
except C: print("C")
except B: print("B")The underscores are spaces.
no, the spaces are spaces. underscores mean something else in python.
(use four spaces at the start of every line for a codeblock. add four more spaces for each level of indentation.)
1
u/Goobyalus May 09 '22
Please clarify your question. What do you expect this to do? What do you understand about wat it does?
1
u/Goobyalus May 09 '22
For future reference you can format a code block by adding an additional 4 spaces before each line, and leaving a blank line before and after the code block. I believe there is a button for this in the Reddit entry box. You could also indent your code block in your text editor by highlighting it and TABing before copying, and undoing to put your code back how it was.