Python初学-划拳游戏


 1 import random
 2 playerScore,computerScore,rounds=0,0,5
 3 for i in range(rounds):
 4 
 5     if playerScore == 3 and computerScore == 0 or playerScore==0 and computerScore==3:
 6         break#连胜直接跳出循环机制
 7     player = int(input('请阁下出拳:'))
 8     computer = random.randint(0, 2)
 9 
10     if player ==0 and computer==1 or player ==1 and computer ==2 or player==2 and computer ==0:
11         print('恭喜你赢了')
12         playerScore+=1
13         pass
14     # 平局机制
15     elif player==computer:
16         print("平局,加赛一局直至分出胜负为止")
17         while player == computer:
18             player = int(input('请阁下出拳:'))
19             computer = random.randint(0, 2)
20             if player == 0 and computer == 1 or player == 1 and computer == 2 or player == 2 and computer == 0:
21                 print('恭喜你赢了')
22                 playerScore += 1
23             elif player != computer:
24                 print('电脑赢了')
25                 computerScore += 1
26     else:
27         print('电脑赢了')
28         computerScore+=1
29     print('现在的比分是:{}:{}\n'.format(playerScore,computerScore))
30 if playerScore>computerScore:
31     print('恭喜您取得了最终的胜利!!!')
32     pass
33 elif playerScore==computerScore:
34     print('\真是个奇迹,你们最终打成了平手/')#不可能的哈哈
35 else :
36     print('电脑取得了最终的胜利!')

2020-05-1009:24

相关