0

I'm learning to program and I'm using "how to think like an computer scientist" the above question is an exercise This is the program without a function

fruit = "banana"

index = 0

while index < len(fruit):

letter = fruit[index]
print(letter)
index = index + 1

I want to put that into a function like

def tranversal(fruit):
    index = 0

    while index < len(fruit):

        letter = fruit[index]
        return letter
        index += 1

        print(tranversal("apple"))

However this is only printing the first letter of "apple" and if I use print statement instead of return I will get None. I'm very confused and need help !!

Sid
  • 2,174
  • 1
  • 13
  • 29

6 Answers6

1

If you use print in the function, then you dont need to use print when calling the function.

def tranversal(fruit):

    index = 0

    while index < len(fruit):

        letter = fruit[index]

        print(letter)

        index += 1

tranversal("apple")

If you use a return statement inside of the while loop, then you will immediately leave the function (and return the first letter), and the while loop will not be executed for higher indices.

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
supinf
  • 302
  • 4
  • 13
1

A simple solution:

print(*'banana', sep='\n')

Output:

b
a
n
a
n
a

With help of the star operator * you can split a list or a string into parts and and pass them as multiple arguments to function. So the expression print(*'abc') is equivalent to print('a', 'b', 'c').

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
1

Seems like you didn't understand the purpose of the return statement inside a function. You might want to read this answer first to make things clear.

Once you understand the difference between print() and return, you should define what your function needs to do. Does it need to return the answer or is printing it on the screen enough?

Assuming the latter, given that strings are iterable, a more pythonic way to do it would be:

def transversal(fruit):
    for letter in fruit:
        print(letter)

Note that since the function is not explicitly returning a value if you try something like:

foo = transversal("banana")

the variable foo will hold the value None.

If you want your function to return the answer and not print it, you could append each letter to an empty result string, with separators for each new line and after you are done with that, simply return result. It could be a good exercise, so you should give it a try :).

vcoutasso
  • 26
  • 3
  • you understand my problem but i'm still very new so wrapping my head around your explanation is a bit difficult and after appending each letter to an empty string i couldnt figure out how to include separators for each new line. – Godswill Okafor Oct 05 '19 at 23:27
  • After each letter, you can add the special character '\n', representing a new line. What's so difficult about my answer? If something isn't clear, you can just ask! – vcoutasso Oct 07 '19 at 02:26
  • you're explanation was very clear, i was just having a hard time understanding but thank you. i've got it and i feel good !!!!!!!!!! – Godswill Okafor Oct 07 '19 at 12:57
  • Glad to know you got it :). If you've found my answer useful, you can mark it as so. Hope you have a great time learning this new language! – vcoutasso Oct 07 '19 at 14:20
  • i don't have enough points to make an upvote yet but when i do i'll definitely mark as so. Thank you for the kind words – Godswill Okafor Oct 08 '19 at 14:40
0

You can use this code snippet

def printAllChar(s):
    for i in s:
        print(i,end='\n')


//calling here...
printAllChar("ProgRank")

//output here...

P
r
o
g
R
a
n
k
0

For the purpose of understanding i wanted to do that exercise with a function, while loop and get a return value. I've gotten help and i appreciate everyone, here is my code:

def `tranversal`(fruit):

   result = ""

   length = int(len(fruit))

   index = 0

   while index < length:

        result += fruit[index]

        index += 1

        if index == length:

            return "\n".join(result)

print(tranversal("string"))
-1

You need to execute the statement using the function outside the function. Just shift return tranversal("apple") outside the function transversal like this:

def transversal(fruit):
    index = 0
    letters = ''

    while index < len(fruit):

        letters += fruit[index] + '\n'
        index += 1
    return letters

print(transversal("apple"))

Thank you @MykolaZotko for pointing out an error in the code that caused it to only print the first letter.

Sid
  • 2,174
  • 1
  • 13
  • 29
  • You have a bug in your function. Try it and see if all letters are printed. You need also to print one letter per line. – Mykola Zotko Oct 08 '19 at 06:26