Reverse: works but adds "None" after my revsered string. Why?


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096

Hi guys, I'm new to Python and I'd like some answers to get a better understanding of how python works.

In the Reverse exercice (see link above) you have to create a function that takes a string and pints it backwards. My code does so but a "None" gets printed after the last letter of the reversed string is printed.

My output for the code below is : !nohtyPNone

Why does it do that and how to solve it ?

Thanks in advance

from __future__ import print_function

def reverse(text):
    for y in range(-1,(-1*len(text))-1,-1):
        print (text[y],end='')



reverse("Python!")

#2

Is the "None" on the next line or combined with the string printed?


#3

Yes exactly like this
Output: !nohtyPNone


#4

This does not look like a solution that would be expected since it is Python 3 and the lesson is Python 2. Did you copy this from another topic?


#5

no I created it but I found the solution to print on the same line from stack overflow


#6

The end='' argument overrides the linebreak. This permits multiline statements to continue printing on the same line.

None is the console response to a return from a function. It can be ignored.


#7

Ok so I should consider the exercise completed although CodeAcademy doesn't think it is. I can always try to come up with another solution until Python considers it completed or copy paste someone else's solution.

Anyway thanks a lot for your answers, much appreciated :slight_smile:


#8

It's a simple enough practice problem that you should be able to come up with a number of working solutions without copying. Can be done with iteration or slicing.

Concerning your approach above, did you try,

reverse("Python!")
print

#9

using the reverse command is not allowed for the exercise, otherwise it'd be too easy. I'll come up with another solution. Thanks again


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.