python实现石头剪刀布程序

时间:2022-11-07 07:42:16

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

概述:

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

流程图:

python实现石头剪刀布程序

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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://blog.csdn.net/lemon_tree12138/article/details/44811299