zun.py
import random
def r(a, b):
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
try:
r(4, 0)
except:
print('Ki yo shi!')
Cela vous donnera le résultat souhaité, mais que faire si vous définissez sauf pour finalement?
zun_2.py
import random
def r(a, b):
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
try:
r(4, 0)
finally:
print('Ki yo shi!')
Message quand enfin
Bouse
Doco
Bouse
Bouse
Bouse
Bouse
Doco
Ki yo shi!
Traceback (most recent call last):
File "zun_2.py", line 5, in <module>
r(4, 0)
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 3, in r
r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
ZeroDivisionError: division by zero
C'est un peu idiot. .. .. Cela ne change pas même si je le fais avec lambda.
zun_2.py
import random
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
try:
r(4, 0)
finally:
print('Ki yo shi!')
Une partie du résultat
Traceback (most recent call last):
File "zun_2.py", line 4, in <module>
r(4, 0)
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
File "zun_2.py", line 2, in <lambda>
r = lambda a,b: r(a-1, print('Bouse')) if random.random() < 0.5 else r(4, print('Doco') or 1/max(a,0))
ZeroDivisionError: division by zero
Hmmm. .. .. En parlant de cela, je me suis demandé si je pouvais appeler la fonction récursive des fonctions anonymes. http://fj.hatenablog.jp/entry/2014/06/15/032135 J'ai vu une histoire comme celle-ci, mais il semble que la performance n'est pas bonne.
Diverses versions propres:
zun_3.py
import random
r=lambda a: print('Bouse') or r(a-1) if random.random() < 0.5 else print('Doco') or a <= 0 or r(4)
r(4) or print('Ki yo shi!')
C'est une excellente chose que si <= est changé en ==, il passera sauf quand il n'y a que quatre dunes. Il devrait y avoir un débat sur la question de savoir s'il est acceptable d'admettre cinq dunes ou plus.