def punctuation(x,y):
if len(x) == 0:
return y
if '.' in x[0]:
x[1] = x[1].capitalize()
return punctuation(x[1:],y.append(x[0]))
elif '!' in x[0]:
x[1] = x[1].capitalize()
return punctuation(x[1:],y.append(x[0]))
elif '?' in x[0]:
x[1] = x[1].capitalize()
return punctuation(x[1:],y.append(x[1]))
else:
return punctuation(x[1:],x[0])
z = ['!a','b']
punctuation(z,[])
希望得到 ['!a','b'] ,这意味着如果第一项包含 (!,?,.),则第二项变为大写
Q3_p1 = "Enter the digit on the phone (0-9): "
Q3_p2 = "Enter the number of key presses (>0): "
def enter_msg(n):
x=raw_input(Q3_p1)
y=raw_input(Q3_p1)
Jay = Jay_chou(x,y)
return Jay
def Jay_chou(d,n):
if d==0: return " "
elif d==1: return [".", ",", "?"][n%3-1]
elif d==2: return ["a", "b", "c"][n%3-1]
elif d==3: return ["d", "e", "f"][n%3-1]
elif d==4: return ["g", "h", "i"][n%3-1]
elif d==5: return ["j", "k", "l"][n%3-1]
elif d==6: return ["m", "n", "o"][n%3-1]
elif d==7: return ["p", "q", "r", "s"][n%4-1]
elif d==8: return ["t", "u", "v"][n%3-1]
else: return ["w", "x", "y", "z"][n%4-1]
enter_msg(2)
我不知道为什么我看到这个错误:
Enter the digit on the phone (0-9): 1
Enter the digit on the phone (0-9): 1
['1', '1']
TypeError: not all arguments converted during string formatting