2010年1月9日 星期六

python 寫 剪刀、石頭、布

# -*- coding: utf-8 -*-
# PYTHON version: 2.5.4

import random

hand = ['剪刀', '石頭', '布']

pwin = 0
cwin = 0

c = random.choice(hand)
pc = input('請選擇-0(剪刀),1(石頭),2(布):')
p = hand[pc]

while pwin < 3 and cwin < 3:
   print '目前成績: 電腦贏了', cwin, '次'
   print ' 玩家贏了', pwin, '次\n\n'

   c = random.choice(hand)
   pc = input('請選擇-0(剪刀),1(石頭),2(布):')
   p = hand[pc]
   if c == '剪刀' and p == '布' or c == '石頭' and p == '剪刀' or c == '布' and p == '石頭':
      print '電腦出:', c, '玩家出:', p
      print 'computer win!'
      cwin += 1

   elif c == '剪刀' and p == '石頭' or c == '石頭' and p == '布' or c == '布' and p == '剪刀':
      print '電腦出:', c, '玩家出:', p
      print 'player win!'
      pwin += 1

   elif c == '剪刀' and p == '剪刀' or c == '石頭' and p == '石頭' or c == '布' and p == '布':
      print '電腦出:', c, '玩家出:', p
      print '平手!!'

if pwin > cwin:
   print '恭禧!玩家勝出!'
else:
   print '你輸了!!'

沒有留言:

張貼留言