码迷,mamicode.com
首页 > 编程语言 > 详细

Python入门——石头剪刀布程序

时间:2015-04-04 06:53:07      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:python   猜拳   

概述:

如果你和我一样是一个有着其他语言基础的编程者,那我想这个小程序对于你来说是小case。由于本人初学Python,就先拿这个熟悉熟悉一下语法,就不再是以前大家都爱用的Hello World了。


流程图:

技术分享


代码如下:

import random

# define a function for get winner
# 1: Scissor
# 2: Stone
# 3: Cloth
def get_winner(you, me):
    if you == me:
        return 0
    
    if you == 1:
        if me == 2:
            return -1
        else:
            return 1

    if you == 2:
        if me == 1:
            return 1
        else:
            return -1

    if you == 3:
        if me == 2:
            return 1
        else:
            return -1

# define a function for get Label for finger
def get_lable(finger):
    if finger == 1:
        return "Scissor"
    elif finger == 2:
        return "Stone"
    else:
        return "Cloth"

you = raw_input("Your Finger is:")
while int(you) > 0:
    you = int(you) % 3
    
    if you == 0:
        you = 3

    me = random.randint(1, 3)
    print "your finger is %s and my finger is %s" % (get_lable(you), get_lable(me))
    
    result = get_winner(you, me)
    
    if result == -1:
        print "I Win."
    elif result == 1:
        print "You Win."
    else:
        print "No winner."
    
    you = raw_input("Your Finger is:")

print "END"



Python入门——石头剪刀布程序

标签:python   猜拳   

原文地址:http://blog.csdn.net/lemon_tree12138/article/details/44811299

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!