Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 18th, 2004, 05:35 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help with concatenating

How do I concatenate or get the out put to print on the same line? I know I need a "+," but it's not working where I think it should.

message = raw_input("Please enter a message to be printed backwards. ")
print

i = 1

while i <= len(message):
new_message = message[-i]
i = i + 1
print new_message

raw_input("\n\nPress the enter key to exit.")

Reply With Quote
  #2  
Old February 18th, 2004, 05:48 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
Originally Posted by briangw
How do I concatenate or get the out put to print on the same line? I know I need a "+," but it's not working where I think it should.

message = raw_input("Please enter a message to be printed backwards. ")
print

i = 1

while i <= len(message):
new_message = message[-i]
i = i + 1
print new_message

raw_input("\n\nPress the enter key to exit.")

Well, there are several ways. An easy way is to put a comma after what you're printing:
Code:
print "This is some text",
This also works great for printing multiple objects of varying type:
Code:
print 1, "is greater than", 0, "but less than", 8
The print with comma syntax will always put a space between your items.

If you want more control on exactly what you are printing, I suggest you read what Mark Pilgrim has to say in Dive Into Python.

Last edited by percivall : February 18th, 2004 at 05:54 PM.

Reply With Quote
  #3  
Old February 18th, 2004, 06:04 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Virginia
Posts: 491 MasterChief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 47 m 47 sec
Reputation Power: 6
Send a message via AIM to MasterChief Send a message via MSN to MasterChief
First of all, you need to indent (four spaces) where it says:

new_message = message[-i]

You're using the plus sign in the proper way. I think I know why your script is not working, though.

Take a look at this:

Code:
01: message = raw_input("Please enter a message to be printed     backwards. ")
02: print
03: 
04: i = 1
05: 
06: while i <= len(message):
07:     new_message = message[-i]
08: i = i + 1
09: print new_message
10: 
11: raw_input("\n\nPress the enter key to exit.")


Okay, look on line four. Now look on line eight. See the problem yet?

You defined i as 1. You then definied i as i + 1. It's going to use the latest definition, and will forget about the i = 1. I would use:

i = 1
i2 = i + 1

Do you understand what I'm saying?

Reply With Quote
  #4  
Old February 18th, 2004, 06:08 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I see now. indenting is something I still have not fully grasped. Created a ton of loops with not doing it properly. Thanks for the input. I see the error of my ways!

Reply With Quote
  #5  
Old February 18th, 2004, 06:13 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by MasterChief
First of all, you need to indent (four spaces) where it says:

new_message = message[-i]

You're using the plus sign in the proper way. I think I know why your script is not working, though.

Take a look at this:

Code:
01: message = raw_input("Please enter a message to be printed     backwards. ")
02: print
03: 
04: i = 1
05: 
06: while i <= len(message):
07:     new_message = message[-i]
08: i = i + 1
09: print new_message
10: 
11: raw_input("\n\nPress the enter key to exit.")


Okay, look on line four. Now look on line eight. See the problem yet?

You defined i as 1. You then definied i as i + 1. It's going to use the latest definition, and will forget about the i = 1. I would use:

i = 1
i2 = i + 1

Do you understand what I'm saying?


I tried what you said, but it locked up Python. I rebooted and it did the same thing. Here's my modified code:

message = raw_input("Please enter a message to be printed backwards. ")
print

i = 1

while i <= len(message):
new_message = message[-i]
i2 = i + 1
print new_message

raw_input("\n\nPress the enter key to exit.")

Isn't the problem with changing the "i" variable to "i2?"

Sorry, the formatting here isn't showing right. I do have the new_message line indented 4 spaces.

Last edited by briangw : February 18th, 2004 at 06:17 PM.

Reply With Quote
  #6  
Old February 18th, 2004, 06:29 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Virginia
Posts: 491 MasterChief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 47 m 47 sec
Reputation Power: 6
Send a message via AIM to MasterChief Send a message via MSN to MasterChief
Hmmm... I'll have to play around with it a bit. It was locking up on me with the original script and the modified script.

Reply With Quote
  #7  
Old February 18th, 2004, 06:43 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
after putting the comma in at the end of the print statement, it worked.

what's interesting is that my professor so far has said that he has seen many different ways (and code) to come to the same conclusion.

Reply With Quote
  #8  
Old February 18th, 2004, 07:07 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
The reason you're code was locking Python up is because 'i' could never be anything but one so the loop could never end...

So all you really needed here was the comma GW . But wait, that leaves a space between each letter which isn't very pretty. But we can use the backspace char (\b) to remove it.

Code:
#!/usr/bin/env python

message = raw_input("Please enter a message to be printed backwards. ")
print

i = 1

while i <= len(message):
    new_message = message[-i]
    i = i + 1   
    print '\b' + new_message,

raw_input("\n\nPress the enter key to exit.")


Of the same thing could be done like this in Python 2.3 using extended slices.

Code:
>>> print raw_input('Enter the word to be Reversed: ')[::-1]
Enter the word to be Reversed: word
drow
>>> 


Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : February 18th, 2004 at 07:10 PM.

Reply With Quote
  #9  
Old February 18th, 2004, 07:17 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No good. He's demanding a "+" in there. He sent me to a chapter on "building a new string" by rebuilding the word, but backwards with no spaces.

My problem is is that I'm confused with how the book is explaining it

Last edited by briangw : February 18th, 2004 at 07:24 PM.

Reply With Quote
  #10  
Old February 18th, 2004, 07:53 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Very simple answer, loop over the string in reverse and concatenate each letter to a new string.. leaving you with a string and all using the + operator.

Code:
>>> string = raw_input('Enter the string to be reversed: ')
>>> output = ''
>>> for letter in string[::-1]:
...     output = output + letter
...     
>>> print output
gnirts
>>> 


If you're not aloud to use extended slices then you'll neet to loop over the number of letters (for each in range(len(string))... or... for index, value in enumerate(string)) and offset the index number by the strings len(). If you want an example let me know?

Also, do you have a link to this book, maybe i can help you understand it .

Mark.

Reply With Quote
  #11  
Old February 18th, 2004, 08:06 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by netytan
Very simple answer, loop over the string in reverse and concatenate each letter to a new string.. leaving you with a string and all using the + operator.

Code:
>>> string = raw_input('Enter the string to be reversed: ')
>>> output = ''
>>> for letter in string[::-1]:
...     output = output + letter
...     
>>> print output
gnirts
>>> 


If you're not aloud to use extended slices then you'll neet to loop over the number of letters (for each in range(len(string))... or... for index, value in enumerate(string)) and offset the index number by the strings len(). If you want an example let me know?

Also, do you have a link to this book, maybe i can help you understand it .

Mark.


Python Programming for the Absolute Beginner, by Michael Dawson, 1-59200-073-8

I tried your code, but got error:

Traceback (most recent call last):
File "C:/Documents and Settings/Brian/Desktop/PYTHON/Class 5/test", line 9, in ?
for letter in message[::-1]:
TypeError: sequence index must be integer

Reply With Quote
  #12  
Old February 18th, 2004, 09:28 PM
briangw briangw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 briangw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Got it!

message = raw_input("Please enter a message to be printed backwards. ")
new_message = ""



for letter in message:
****new_message = letter + new_message
print new_message

raw_input("\n\nPress the enter key to exit.")

I hate programming. It's required for my network admin degree. Thanks for your help. Midterms are coming up next week and we have some programs to write, so I may be calling on some help again.

Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with concatenating


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |