圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了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"

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

标签:
python,石头剪刀布

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com