分析:
用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
法一:
letter = input("请输入:")
if letter == 'S':
print('请输入第二个字符:')
letter = 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 = 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')
法二:
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=input('请输入第二个字母:')
for b in li:
if re.match(first+second,b):
print(b)
list=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
first=input('请输入第一个字母:')
judge(first,list)
法三:
使用递归函数实现,对字符相同的个数没有限制:
week=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
def find(s,week):
a = input("please input a letter")
s=s+a
result=[]
for i in week:
if i.startswith(s):
result.append(i)
if len(result)==1:
return result
else:
return find(s,result)
s=''
print(find(s,week))