《python核心編程》(第二版)第六章課后習題 6-14 p162
def Rochambeau(): while True: print """請輸入選項: 1 布 2 石頭 3 剪子 4 退出""" while True: player = raw_input() if player not in '1234': print '選項不正確,重新輸入' else: break player = int(player) if player==4: break robot = random.choice([1,2,3]) print 'versus ',robot rules1 = {1:'E',2:'P',3:'R'} rules2 = {1:'R',2:'E',3:'P'} rules3 = {1:'P',2:'R',3:'E'} rules = {1:rules1,2:rules2,3:rules3} score = rules[player][robot] res = {'P':'你贏啦','R':'你輸啦','E':'平局'} print res[score]
運行結果: