# -*- coding: utf-8 -*-
# Python version: 2.5.4
import random
ans = random.randint(0, 3)
count= 0
num = 0
python學習心得、練習、教學 易於學習、功能強大,語法清晰易讀, 方便後續維護,與其它語言結合性強。 如jython(java)、ironpython(visual studio),等於可拿別的語言優點來做結合,又有個別稱『膠水語言』
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 '你輸了!!'
# 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 '你輸了!!'
訂閱:
意見 (Atom)