这是我的编码:
def cookie_cook2(cook):
""" cookie_cook2 takes a string `cook` and returns a string containing a grammatically correct and important question about the cook """
# code here.
if cook[0].lower()=="a" or cook[0].lower()=="e" or cook[0].lower()=="i" or cook[0].lower()=="o" or cook[0].lower()=="u":
return 'How many cookies could an '+ cook + ' cook if a good cook could cook cookies'
else:
return 'How many cookies could a ' +cook+ ' cook if a good cook could cook cookies'
但是如何我输入>>> print(cookie_cook2(""))
它产生额结果不是How many cookies could a cook if a good cook could cook cookies 而是Your submission raised an exception of type IndexError
.
# 要先判断cook字符串长度>=1,再进行下一步判断
if len(cook)>=1 and (cook[0].lower()=="a" or cook[0].lower()=="e" or cook[0].lower()=="i" or cook[0].lower()=="o" or cook[0].lower()=="u"):
cook[0] 是取字符串中第一个字符,但是你给cook传的是空字符串"",字符串中没有字符,怎么取第一个字符啊