python2.7练习小例子(二十八)
Posted luyaran
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于python2.7练习小例子(二十八)的主要内容,如果未能解决你的问题,请参考以下文章