猜驴 分类: python 小练习 2013-06-28 10:37 216人阅读 评论(0) 收藏
import random
door =['A','B','C']
dunkey = random.choice(door)
print "驴在:",dunkey
while 1:
user = raw_input("choose it among A-C:").title()
if user not in ['A','B','C']:
print "请在A、B、C中选择一个门"
continue
break
print "你第一次选择了 %s" % user
while 1:
i = random.choice(door)
if i!=dunkey and i!=user:
break
door.remove(i)
print '系统删除了',i,"剩下了",door
tag = raw_input("你要更换你的选择吗?Y/N")
if tag in ('n','N'):
if dunkey==user:
print "祝贺你"
else:
print "不更换选项操作,猜错了,答案应该是:%s" % dunkey
if tag in ('y','Y'):
new = ''.join([k for k in door if k!=user]).title()
print "你更换了选项,并选择了 %s" % new
if new ==dunkey:
print "祝贺你"
else:
print "猜错了,答案应该是:%s" % dunkey
===========================================================
使用函数:
import random
door =['A','B','C']
dunkey = random.choice(door)
print "驴在:",dunkey
user = ''
#得到用户的选择:
def userInput():
global user
while 1:
user = raw_input("choose it among Door:").title()
if user not in door:
print "请在A、B、C中选择一个门"
continue
break
print "你第一次选择了 %s" % user
#删除一个不包含驴的选项
def delblank():
while 1:
i = random.choice(door)
if i!=dunkey and i!=user:
door.remove(i)
break
print '系统删除了',i,"剩下了",door
def changeSelected():
tag = raw_input("你要更换你的选择吗?Y/N")
if tag in ('n','N'):
if dunkey==user:
print "祝贺你"
else:
print "您没有更换选项,猜错了,答案应该是:%s" % dunkey
if tag in ('y','Y'):
new = ''.join([k for k in door if k!=user]).title()
print "你更换了选项,并选择了 %s" % new
if new ==dunkey:
print "祝贺你"
else:
print "猜错了,答案应该是:%s" % dunkey
userInput()
delblank()
changeSelected()
版权声明:本文为博主原创文章,未经博主允许不得转载。