python2.7练习小例子(二十八)

    28):题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

    程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。

 


#!/usr/bin/python
# -*- coding: UTF-8 -*-

letter = raw_input("please input:")
#while letter  != 'Y':
if letter == 'S':
    print ('please input second letter:')
    letter = raw_input("please input:")
    if letter == 'a':
        print ('Saturday')
    elif letter  == 'u':
        print ('Sunday')
    else:
        print ('data error')
    
elif letter == 'F':
    print ('Friday')
    
elif letter == 'M':
    print ('Monday')
    
elif letter == 'T':
    print ('please input second letter')
    letter = raw_input("please input:")
 
    if letter  == 'u':
        print ('Tuesday')
    elif letter  == 'h':
        print ('Thursday')
    else:
        print ('data error')
        
elif letter == 'W':
    print ('Wednesday')
else:
    print ('data error')

    以上实例输出结果为:

 

please input:S
please input second letter:
please input:a
Saturday

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import  re

def judge(first,list):
    li=[]
    first = first.upper()
    for a in list:
        if re.match(first,a):
            li.append(a)
    if len(li)==1:
        print li[0]
    else:
        second=raw_input('请输入第二个字母:')
        for b in li:
            if re.match(first+second,b):
                print b

list=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
first=raw_input('请输入第一个字母:')
judge(first,list)

    Python3 测试实例:

 

#!/usr/bin/python3

weeklist = {'M': 'Monday','T': {'u': 'Tuesday','h':'Thursday'}, 'W': 'Wednesday', 'F':'Friday','S':{'a':'Saturday','u':'Sunday'}}
sLetter1 = input("请输入首字母:")
sLetter1 = sLetter1.upper()

if (sLetter1 in ['T','S']):
    sLetter2 = input("请输入第二个字母:")
    print(weeklist[sLetter1][sLetter2])
else:
    print(weeklist[sLetter1])

    Python3实例,使用递归函数实现,对字符相同的个数没有限制:

 

#!/usr/bin/env python3

week=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

def find(s,week):
    a = input("please input a letter\n")
    s=s+a
    #print(s)    
    result=[]
    for i in week:
        if i.startswith(s):
            result.append(i)
    #print (result)    
    if len(result)==1:
        return result
    else:
        return find(s,result)

s=''
print(find(s,week))

    情况语句练习小例子。如果感觉不错的话,请多多点赞支持哦。。。

  原文链接:https://blog.csdn.net/luyaran/article/details/80075427

posted @ 2018-04-25 13:18  luyaran  阅读(292)  评论(0编辑  收藏  举报