Python石头剪子布简单程序

我之前写过一篇基于JS的石头剪子布程序 《JavaScript写的一个石头剪子布游戏,大家进来玩玩吧!》,今天又基于Python写了一个实例,这里边的算法有点特殊但一时也想不到一个好的算法规律。

Python Code

# encoding=UTF-8
# 石头剪子布 程序
import random
 
# 定义石头剪子布字典
dict = {1:'剪子',2:'石头',3:'布'}
 
for row in dict:
  print '编号:',row,' = ',dict[row]
 
print '您出什么?'
 
loop = True
while loop:
  you = raw_input('请输入编号回车: ')
  try:
    you = int(you)
    if you>=1 and you<=3:
      loop = False
    else:
      print '请输入 1-3 范围内的编号'
  except Exception,e:
    print '请输入正确的数字编号'
 
dn = random.randint(1,3)
print '你出:',dict[you]
print '电脑出:',dict[dn]
print '结果:',
 
if dn==you:
  print '平局'
elif (you>dn and you-dn==1) or you+2==dn:
  print '你胜'
else:
  print '电脑胜'

Run:

编号: 1  =  剪子
编号: 2  =  石头
编号: 3  =  布
您出什么?
请输入编号回车: 3
你出: 布
电脑出: 石头
结果: 你胜
分享

TITLE: Python石头剪子布简单程序

LINK: https://www.qttc.net/289-python-rock-paper-scissors.html

NOTE: 原创内容,转载请注明出自琼台博客