Execute HQ9+/Python: Difference between revisions

Replace if ... elif ... elif ... with dispatch table.
(no need to remark on +)
(Replace if ... elif ... elif ... with dispatch table.)
Line 36:
print "99 bottles of beer on the wall."
print
 
def incr():
acc += 1 # ?
 
if len(sys.argv) != 2:
Line 51 ⟶ 54:
acc = 0
src = s.read()
 
# Implement interpreter using a dispatch table
dispatch = {
'h': hello,
'q': quine(),
'9': bottles(),
'+': incr
else: }
 
for i in src.lower():
if i ==in 'h'dispatch:
hellodispatch[i]()
elif i == 'q':
quine()
elif i == '9':
bottles()
elif i == '+':
acc += 1 # ?
else:
pass # It's a comment, ignore it.</lang>
Anonymous user