range(N, -1)

I am learning using Pycharm and Python Programming 3rd edition for absolute beginners.
Am at Chapter 4, (not far along , I know) and I,m stuck.
In a challenge you have to write a program that get a message from the user and then print it out backwards.
I have managed to write a program that seems to work but, the first letter of the message is always printed first and not last as expected.
If I ad a space at the beginning of the message then it prints as expected, of course you don't see the beginning space.
I am missing something or doing something wrong.   I can't move on until I've sorted it.
Hope somebody can help me.  I have attached the program.
Thanks
3 comments
Comment actions Permalink
I don't see your program attached.  But based on the subject, I'm guessing the problem might be incorrect arguments to range(). If you need to specify a stepping increment there have to be 3 arguments, not 2.
0
Comment actions Permalink
The file was a Mac Pages file.  Maybe I'll put it all on -print ("\n\nI am going to print a message that you give me BACKWARDS  - ")

message = input("Your message please   
         ?  ",)
print ("\nMessage",len(message))
print()
mes =  int(len(message))
for i in range(mes):
    print (message [-i], end=" ")

print("\n\n\nIf I add a a space at the begining of the message this is the result -")
print ("Not sure what I'm missing")
print("\n")

  1.    The first section the length of the message is 9 chars
  2.    in the second section the length of the message is 10 chars (an additional SPACE has been added)
  3.    Why is this so, is it something to do with the length of the string 9 as opposed to 10?
message = (input("Your message please   
           ?  "))
mes = int(len(message))
print("\n\n")
print (message, mes)
for i in range(mes):
    print (message [-i], end= " ")
print ("\n\n")
for i in range(10, 0, -1):
    print (i ,end=" ")

input ("\nPress the Enter Key to exit.")
0
Comment actions Permalink
try using   { code}  (without leading space before "code") to format your code, look at http://forum.jetbrains.com/wikiHelpWrapper for Code Formatting


but like Preston said relating to range, try this:

for i in range(0,mes,-1):
    print message[i],



Also, you don't have to turn len(message) into an int, as len() returns an int.
0

Please sign in to leave a comment.