Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list - 2 questions:
#36
For loop or while loop doesn't matter. If you can write code using a for loop you can write code to do the same thing using a while loop. I started with the while loop and progressed through the for loop because it naturally leads to the range() function and the range function is a lot like slicing a list.

You do not need to put the words in a list. You need to put the words in a string. The function returns a string. The only reason for putting the words in a list is it makes it easier to make the string if you only have to worry about the even words.

Getting the even words is only half the solution. According to your first post the function is supposed to return a string like "hydrogen, lithium and boron". Now that you have the even words, how are you going to make the string?

This is easy if you know there are 3 words in the list:
def format_list(my_list):
    even_words = my_list[::2]
    return even_words [0] + ', ' + even_words [1] + ' and ' + even_words [2]
But your function cannot be written to only work if my_list has 6 or 7 words, it has to work for any length my_list.
Reply


Messages In This Thread
list - 2 questions: - by ben1122 - Jul-31-2021, 11:33 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 11:36 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:41 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:43 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 11:46 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:48 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:44 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 11:45 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:47 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:05 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:08 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:10 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:16 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:17 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:17 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:19 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:23 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:31 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:37 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:39 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:37 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:40 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:49 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:50 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:53 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 01:00 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 01:03 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 01:24 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 01:32 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 01:36 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 02:03 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 02:05 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 02:24 PM
RE: list - 2 questions: - by deanhystad - Jul-31-2021, 03:11 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 03:31 PM
RE: list - 2 questions: - by deanhystad - Jul-31-2021, 05:06 PM
RE: list - 2 questions: - by Pedroski55 - Jul-31-2021, 11:18 PM
RE: list - 2 questions: - by Pedroski55 - Jul-31-2021, 11:48 PM
RE: list - 2 questions: - by Pedroski55 - Aug-01-2021, 03:06 AM
RE: list - 2 questions: - by snippsat - Aug-01-2021, 04:27 PM
RE: list - 2 questions: - by naughtyCat - Aug-18-2021, 05:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 48,849 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020