英文:
Automate the boring stuff with Python - Comma Code exercise
问题
PS:这本书还没有涉及到使用join函数,所以你必须使用if、elif和else。之前问过这个问题,但这只是第2-3章,我们还没有用到join函数。
所以在《自动化繁琐的事情中的逗号代码练习》中,你需要创建一个列表,并在每个项目之间使用逗号打印它们,并在最后一个项目之前添加“和”。
示例:
spam = ['apples', 'bana', 'apple']
def commacode(passedlist):
stringy = ''
if len(passedlist) == 1:
stringy += str(passedlist[0])
return stringy
for i in range(len(passedlist) - 1):
stringy += str(passedlist[i]) + ', '
# 添加除最后一个项目外的所有项目到字符串中
stringy += 'and ' + str(passedlist[len(passedlist) - 1])
# 在“and”之后添加最后一个项目到字符串中
return stringy
print(commacode(spam))
输出结果是:apples, bana, and apple。
然而,如果只有两个项目,它将打印出“apples, and bana”,这在语法上是不正确的。所以我的问题是,我该如何使它正确?我尝试添加elif和else语句,但它破坏了我的代码,我没有保存它来展示给你们(抱歉)。
英文:
PS The book doesn't reach the join function yet so you have to use if,elif and else. Asked the question before but this is only like chapter 2-3 guys, We haven't gotten to use join yet
So in the comma code exercise in automate the boring stuff you have to create a list and print it out using commas between each item and add 'and' before the last item.
Example
spam = ['apples','bana', 'apple']
def commacode(passedlist):
stringy = ''
if len(passedlist) == 1:
stringy += str(passedlist[0])
return stringy
for i in range(len(passedlist)-1):
stringy += str(passedlist[i]) + ', '
# adds all except last item to str
stringy += 'and ' + str(passedlist[len(passedlist)-1])
# adds last item to string, after 'and'
return stringy
print(commacode(spam))
outputs apples , bana, and apple.
However if it is only two items it would print out apples, and bana which isnt grammatically correct. So my question is how would I make it correct I tried adding a elif and else statement but it broke my code and I didn't save it to show you guys(sorry)
答案1
得分: 2
以下是翻译好的代码部分:
def commacode(words):
if len(words) == 1:
return words[0]
if len(words) == 2:
return f"{words[0]} and {words[1]}"
ret = ""
for word in words[:-1]:
ret += f"{word}, "
return ret + f"and {words[-1]}"
print(commacode(["spam"])) # spam
print(commacode(["spam", "eggs"])) # spam and eggs
print(commacode(["spam", "eggs", "spam", "spam"]))
# spam, eggs, spam, and spam
请注意,代码部分已翻译,其余部分未翻译。
英文:
It's easiest to handle 1- and 2-length lists as special cases. They're both simple enough that you can take care of them in a single line of code (with no looping involved), and then loop to handle all the remaining cases (which are all done in the same way):
def commacode(words):
if len(words) == 1:
return words[0]
if len(words) == 2:
return f"{words[0]} and {words[1]}"
ret = ""
for word in words[:-1]:
ret += f"{word}, "
return ret + f"and {words[-1]}"
print(commacode(["spam"])) # spam
print(commacode(["spam", "eggs"])) # spam and eggs
print(commacode(["spam", "eggs", "spam", "spam"]))
# spam, eggs, spam, and spam
答案2
得分: 1
一个elif和else语句是你需要的,你的想法是正确的。
英文:
An elif, and else statement is what you need, you were on the right track.
spam = ['apples','banana','apple']
def commacode(passedlist):
stringy = ''
if len(passedlist) == 1:
stringy += str(passedlist[0])
return stringy
# added the elif here to check if list is only 2 objects
elif len(passedlist) == 2:
# this adds the strings together
stringy = passedlist[0] + ' and ' + passedlist[1]
# else statement is required so this code only runs
# if the list is not of length 1 or 2
else:
for i in range(len(passedlist)-1):
stringy += str(passedlist[i]) + ', '
# adds all except last item to str
stringy += 'and ' + str(passedlist[len(passedlist)-1])
# adds last item to string, after 'and'
return stringy
print(commacode(spam))
You had the right idea with the elif and else statement, I would just imagine that you might have had indentations wrong or something.
答案3
得分: 0
spam = ['apples', 'bana', 'apple']
def commacode(passedlist):
stringy = ''
for i in range(len(passedlist)):
if i == len(passedlist) - 1 and i > 0:
stringy += " and "
stringy += passedlist[i]
if i < len(passedlist) - 2:
stringy += ", "
return stringy
print(commacode(spam))
英文:
spam = ['apples', 'bana', 'apple']
def commacode(passedlist):
stringy = ''
for i in range(len(passedlist)):
if i==len(passedlist)-1 and i>0:
stringy += " and "
stringy += passedlist[i]
if i<len(passedlist)-2:
stringy+= ", "
return stringy
print(commacode(spam))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论