白云岛资源网 Design By www.pvray.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"

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

白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com