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"



原文地址:https://www.cnblogs.com/fengju/p/6336099.html