python_利用元组实现剪刀石头布

《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]

运行结果:

原文地址:https://www.cnblogs.com/AHappyBird/p/9414556.html