猜驴 分类: 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()




版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2013-06-28 10:37  前行者2011  阅读(115)  评论(0编辑  收藏  举报