Python Forum
[Guidance needed] Python def question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Guidance needed] Python def question
#1
Complete the program (i.e. implement the function concat_words ) so that it will print the
output as shown:
[1-apple*2-orange*3-pear*]

Answer:




all_words = concat_words(['apple', 'orange', 'pear'])
print ('[' + all_words + ']')
Reply
#2
What have you tried?
Reply
#3
(Aug-04-2019, 06:15 AM)ndc85430 Wrote: What have you tried?
def concat_words(a,b,c):
    

all_words = concat_words(['apple', 'orange', 'pear'])
print ('[' + all_words + ']')
idk man, im stucked. Please advise!
Reply
#4
So you've tried nothing? How long have you been learning Python? Do you know how lists work? Where did this problem come from? If it's from a class or something, you probably need to speak to your teacher if you're struggling this much.
Reply
#5
It looks like concat_words is meant to take one parameter, a list, not three parameters as you have it set up.

Take a look at this example, and see if it helps you get there:

words = ['apple', 'orange', 'pear']
for index in range(len(words)):
    print(index, words[index])
Really this should be done with enumerate, but I have a feeling you haven't covered that yet:

words = ['apple', 'orange', 'pear']
for index, word in enumerate(words):
    print(index, word)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Python Homework (Urgent help needed!!) chickenseizuresalad 6 4,217 Oct-11-2021, 01:59 AM
Last Post: Underscore
  Need guidance on String indexing Gateux 3 2,119 Jun-22-2019, 12:44 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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