该表达式
this_prize.choice告诉解释器您要访问名称为“
choice”的this_prize属性。但是此属性在this_prize中不存在。
您真正想要的是返回由选择 值 标识的this_prize属性。所以您只需要更改最后一行…
from collections import namedtupleimport randomPrize = namedtuple("Prize", ["left", "right" ])this_prize = Prize("FirstPrize", "SecondPrize")if random.random() > .5: choice = "left"else: choice = "right"#retrieve the value of "left" or "right" depending on the choiceprint "You won", getattr(this_prize,choice)


