ifブロック内でメソッドを呼び出すとTypeErrorが発生しますが、"TypeError:if文(Python)でメソッドが呼び出されたときに 'str'オブジェクトが呼び出されません '
randenzyme()ランダムな酵素と呼ばれる辞書からキーを選択します:ブロックがあれば外に
def randenzyme(self):
an_enzyme = choice(self.enzymes.keys())
それはで正しく動作します:
x = TCA()
x.randenzyme()
しかし、「例外TypeError: 'str' はオブジェクトがありますコール可能ではない " with:
x = TCA()
user_input = raw_input('> ')
if user_input == "1":
x.randenzyme()
ifブロックでメソッドを呼び出すとどうなりますか?
UPDATE:ここでは、完全なコードです
from random import *
import sys
class TCA(object):
def __init__(self):
self.enzymes = {}
self.enzymes['citrate synthase'] = ('oxaloacetate', 'citrate')
self.enzymes['aconitase'] = ('citrate', 'isocitrate')
self.enzymes['isocitrate dehydrogenase'] = ('isocitrate', 'alpha-ketoglutarate')
self.enzymes['alpha-ketoglutarate dehydrogenase'] = ('alpha-ketoglutarate', 'succinyl-CoA')
self.enzymes['succinyl-CoA synthetase'] = ('succinyl-CoA', 'succinate')
self.enzymes['succinate dehydrogenase'] = ('succinate', 'fumarate')
self.enzymes['fumarase'] = ('fumarate', 'malate')
self.enzymes['malate dehydrogenase'] = ('malate', 'oxaloacetate')
def randenzyme(self):
an_enzyme = choice(self.enzymes.keys())
print "Reaction (enzyme): %s" % an_enzyme
return an_enzyme
x = TCA()
print 'Enter "1" for a random enzyme or "2" to exit'
choice = raw_input("> ")
if choice == '1':
x.randenzyme()
elif choice == '2':
sys.exit()
私が手にエラーがある:
Traceback (most recent call last):
File "/Users/sloria1/TCASO.py", line 24, in <module>
x.randenzyme()
File "/Users/sloria1/TCASO.py", line 16, in randenzyme
an_enzyme = choice(self.enzymes.keys())
TypeError: 'str' object is not callable
不十分な情報です。メソッドを呼び出す方法は、これらの2つのケースでまったく同じにする必要があります。 – wim