当前位置:首页 >> 脚本专栏

python实现石头剪刀布程序

本文实例为大家分享了python实现石头剪刀布的具体代码,供大家参考,具体内容如下

概述:

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

流程图:

python实现石头剪刀布程序

代码如下:

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"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。