上班摸鱼写了个石头剪刀布的程序:
import random
import time
print('----------石头剪刀布----------')
times = time_a = int(input('请输入回合数:'))
win_p = win_n = draw = 0 #玩家与电脑的胜利回合数
pc = ['石头','剪刀','布']
while times > 0:
print('第',time_a-times+1,'回合')
time.sleep(1)
while True:
player = input('石头 剪刀 布:')
if player in ['石头','剪刀','布']:
break
num = random.randint(0,2)
npc = pc[num] #电脑随机出拳
time.sleep(1)
print('NPC出的是:',npc)
time.sleep(1)
if (player == '石头' and npc == '剪刀') or\
(player == '剪刀' and npc == '布') or\
(player == '布' and npc == '石头'):
print('玩家胜')
win_p += 1
elif player == npc:
print('平局')
draw += 1
else:
print('电脑胜')
win_n += 1
times -= 1
time.sleep(2)
if win_p > win_n:
winer = '玩家获胜'
elif win_p < win_n:
winer = '电脑获胜'
else:
winer = '平局'
print()
print(winer+'\n'+'玩家胜回合数:',win_p,' 电脑胜回合数:',win_n,' 平局回合数',draw)
判定输赢这块因为是三角关系不好用数值比较就只能把结果都列出来,有没有什么更巧妙的写法:
if (player == '石头' and npc == '剪刀') or\
(player == '剪刀' and npc == '布') or\
(player == '布' and npc == '石头'):
print('玩家胜')
win_p += 1
elif player == npc:
print('平局')
draw += 1
else:
print('电脑胜')
win_n += 1