python 实现剪刀石头布(三局两胜)

 1 # -*- coding:utf-8 -*-
 2 import random
 3 
 4 # best of three
 5 def finger_guess():
 6     rule = {1:'rock', 2:'paper', 3:'scissor'}
 7     win_way = [['rock', 'scissor'], ['paper', 'rock'], ['scissor', 'paper']]
 8     num_list = [1, 2, 3]
 9     count = 0
10     person_score = 0
11     computer_score = 0
12     while count < 3:
13         person = raw_input('please input your choice:
1.rock
2.paper
3.scissor
')
14         computer = random.choice([1, 2, 3])
15         try:
16             person = int(person)
17             if person in num_list:
18                 print 'your: %s, computer: %s' %(rule[person], rule[computer])
19                 if rule[person] == rule[computer]:
20                     print 'Same! One more try!'
21                     continue
22                 for item in win_way:
23                     if rule[person] == item[0] and rule[computer] == item[1]:
24                         print 'Win once! Come on!'
25                         person_score += 1
26                     if rule[person] == item[1] and rule[computer] == item[0]:
27                         print 'Lose once! Never mind!'
28                         computer_score += 1
29             else:
30                 print 'Are you kidding me! Please respect the Holy Game!'
31                 continue
32             count += 1
33             if computer_score == 2 or person_score == 2:
34                 break
35         except ValueError:
36             print 'Please input num in [1, 2, 3], Stupid!'
37     return person_score, computer_score
38 
39 print 'This is a game called finger_guess, you have three choices.
Wanna beat the AI, let us try!'
40 person_score, computer_score = finger_guess()
41 print 'final score:
your:%d computer:%d' %(person_score, computer_score)
42 if person_score > computer_score:
43     print 'You get it'
44 else:
45     print 'You are so pussy!!'
原文地址:https://www.cnblogs.com/laresh/p/6526422.html