お世話になります
下記のスクリプトを記述するとエラーになります。
どこかが間違っているのでしょうか?
>>> import threading >>> balance = 100 >>> class LazyThread(threading.Thread): ... def run(self): ... import time ... time.sleep(1) ... try: ... withdraw_with_confirm(90) ... except ValueError: ... print('lazy_thread: NG %s' % balance) ... else: ... print('lazy_thread: OK %s' %balance) ... >>> lazy_thread = LazyThread() >>> lazy_thread.start() >>> Exception in thread Thread-1: Traceback (most recent call last): File "C:\Users\mtg-1\Anaconda2\lib\threading.py", line 801, in __bootstrap_inner self.run() File "<stdin>", line 6, in run NameError: global name 'withdraw_with_confirm' is not defined >>> try: ... withdraw_with_confirm(20) ... except ValueError: ... print('current_thread: NG %s' % balance) ... else: ... print('current_thread: OK %s' % balance) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> NameError: name 'withdraw_with_confirm' is not defined
ちなみに#withdraw.pyを作ってあります ```ここに言語を入力 # -*- coding: utf-8 -*- import threading lock = threading.Lock() balance = 0 def withdraw_with_confirm(money): global balance lock.acquire() try: if balance >=money: if raw_input('y/n?: ') == 'y': balance -= money return True return False else: raise ValueError() finally: lock.release()
python withdraw.pyを実行しても
y/n?: y
Current_thread: OK 80
lazy_thread: NG 80
このように表示されるところですが何も表示されません
何度もやり直しても解決できないのでご教授いただけないでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/23 07:28
2017/03/23 08:14
2017/03/24 00:12
2017/03/24 01:05
2017/03/24 03:48
2017/03/24 05:05
2017/03/24 05:48