自动化无聊的事情使用Python – 逗号代码练习

huangapple go评论101阅读模式
英文:

Automate the boring stuff with Python - Comma Code exercise

问题

PS:这本书还没有涉及到使用join函数,所以你必须使用if、elif和else。之前问过这个问题,但这只是第2-3章,我们还没有用到join函数。

所以在《自动化繁琐的事情中的逗号代码练习》中,你需要创建一个列表,并在每个项目之间使用逗号打印它们,并在最后一个项目之前添加“和”。

示例:

  1. spam = ['apples', 'bana', 'apple']
  2. def commacode(passedlist):
  3. stringy = ''
  4. if len(passedlist) == 1:
  5. stringy += str(passedlist[0])
  6. return stringy
  7. for i in range(len(passedlist) - 1):
  8. stringy += str(passedlist[i]) + ', '
  9. # 添加除最后一个项目外的所有项目到字符串中
  10. stringy += 'and ' + str(passedlist[len(passedlist) - 1])
  11. # 在“and”之后添加最后一个项目到字符串中
  12. return stringy
  13. 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

  1. spam = ['apples','bana', 'apple']
  2. def commacode(passedlist):
  3. stringy = ''
  4. if len(passedlist) == 1:
  5. stringy += str(passedlist[0])
  6. return stringy
  7. for i in range(len(passedlist)-1):
  8. stringy += str(passedlist[i]) + ', '
  9. # adds all except last item to str
  10. stringy += 'and ' + str(passedlist[len(passedlist)-1])
  11. # adds last item to string, after 'and'
  12. return stringy
  13. 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

以下是翻译好的代码部分:

  1. def commacode(words):
  2. if len(words) == 1:
  3. return words[0]
  4. if len(words) == 2:
  5. return f"{words[0]} and {words[1]}"
  6. ret = ""
  7. for word in words[:-1]:
  8. ret += f"{word}, "
  9. return ret + f"and {words[-1]}"
  10. print(commacode(["spam"])) # spam
  11. print(commacode(["spam", "eggs"])) # spam and eggs
  12. print(commacode(["spam", "eggs", "spam", "spam"]))
  13. # 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):

  1. def commacode(words):
  2. if len(words) == 1:
  3. return words[0]
  4. if len(words) == 2:
  5. return f"{words[0]} and {words[1]}"
  6. ret = ""
  7. for word in words[:-1]:
  8. ret += f"{word}, "
  9. return ret + f"and {words[-1]}"
  10. print(commacode(["spam"])) # spam
  11. print(commacode(["spam", "eggs"])) # spam and eggs
  12. print(commacode(["spam", "eggs", "spam", "spam"]))
  13. # spam, eggs, spam, and spam

答案2

得分: 1

  1. 一个elifelse语句是你需要的,你的想法是正确的。
英文:

An elif, and else statement is what you need, you were on the right track.

  1. spam = ['apples','banana','apple']
  2. def commacode(passedlist):
  3. stringy = ''
  4. if len(passedlist) == 1:
  5. stringy += str(passedlist[0])
  6. return stringy
  7. # added the elif here to check if list is only 2 objects
  8. elif len(passedlist) == 2:
  9. # this adds the strings together
  10. stringy = passedlist[0] + ' and ' + passedlist[1]
  11. # else statement is required so this code only runs
  12. # if the list is not of length 1 or 2
  13. else:
  14. for i in range(len(passedlist)-1):
  15. stringy += str(passedlist[i]) + ', '
  16. # adds all except last item to str
  17. stringy += 'and ' + str(passedlist[len(passedlist)-1])
  18. # adds last item to string, after 'and'
  19. return stringy
  20. 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

  1. spam = ['apples', 'bana', 'apple']
  2. def commacode(passedlist):
  3. stringy = ''
  4. for i in range(len(passedlist)):
  5. if i == len(passedlist) - 1 and i > 0:
  6. stringy += " and "
  7. stringy += passedlist[i]
  8. if i < len(passedlist) - 2:
  9. stringy += ", "
  10. return stringy
  11. print(commacode(spam))
英文:
  1. spam = [&#39;apples&#39;, &#39;bana&#39;, &#39;apple&#39;]
  2. def commacode(passedlist):
  3. stringy = &#39;&#39;
  4. for i in range(len(passedlist)):
  5. if i==len(passedlist)-1 and i&gt;0:
  6. stringy += &quot; and &quot;
  7. stringy += passedlist[i]
  8. if i&lt;len(passedlist)-2:
  9. stringy+= &quot;, &quot;
  10. return stringy
  11. print(commacode(spam))

huangapple
  • 本文由 发表于 2023年6月16日 02:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定