|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. ") 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.") |
|
#2
|
|||
|
|||
|
Quote:
Well, there are several ways. An easy way is to put a comma after what you're printing: Code:
print "This is some text", Code:
print 1, "is greater than", 0, "but less than", 8 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. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
|||
|
|||
|
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!
![]() |
|
#5
|
|||
|
|||
|
Quote:
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. ") 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. |
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
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. Last edited by netytan : February 18th, 2004 at 07:10 PM. |
|
#9
|
|||
|
|||
|
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. |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
|||
|
|||
|
Quote:
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 |
|
#12
|
|||
|
|||
|
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! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with concatenating |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|