Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String Manipulation Exercise (Generating an nXn box)
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sasarai




PostPosted: Wed Jul 22, 2009 12:33 am   Post subject: String Manipulation Exercise (Generating an nXn box)

Hello there fellow programmers. I've been working through McKenzie's exercises and lessons on Python and I've been struggling on a string exercise. It simply requires an input "n" from the user and generates an nXn box. Now, strings are unit 2.2 as far as I know, and I can't seem to figure out how I could generate such a thing simply using string techniques and Basic IO. After surfing the internet for a while, I did come across a way to tackle this problem using Loops (which is 2.4 so I hadn't even touched it yet @.@), but if there's a way simply using strings, I'd definately like to learn it. Thank you for your patience and time. ^^.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Wed Jul 22, 2009 12:41 am   Post subject: RE:String Manipulation Exercise (Generating an nXn box)

I don't have the structure of McKenzie's exercises memorized, but I think what you're looking for is the following:
code:
print "Hi" * 4
Sasarai




PostPosted: Wed Jul 22, 2009 12:49 am   Post subject: Re: String Manipulation Exercise (Generating an nXn box)

Thanks for the reply Gandalf ^^. I did come across that helpful function you posted in McKenzie's note. However, I'm struggling with how I can generate the "box".

as of now I have:

code:
numero = input("Please enter a number")
row = numero*"X"


But from that point, this is where I get stuck. I had thought and foolishly tried doing something along the lines of:

code:
numero = input("Please enter a number")
row = numero*"Y"
print row.replace('Y', 'numero*"X" \n')


Only to realize that it wouldn't work. T_T

I considered making a string of n*2 length and trying to slice it...but it got rather complicated @.@.
[Gandalf]




PostPosted: Wed Jul 22, 2009 12:53 am   Post subject: RE:String Manipulation Exercise (Generating an nXn box)

No problem. Smile

Think of how you can combine a newline character "\n", with string multiplication, as well as ( ) order of operations to create a box. Those should be all that is required, no need for explicit string functions such as replace().
Sasarai




PostPosted: Wed Jul 22, 2009 1:31 am   Post subject: Re: String Manipulation Exercise (Generating an nXn box)

Thanks for the advice Gandalf, I'll keep on trying ^^b.
[Gandalf]




PostPosted: Wed Jul 22, 2009 2:13 am   Post subject: RE:String Manipulation Exercise (Generating an nXn box)

As a quick guess; the new line character should always be in "string quotes\n", otherwise \ acts as a line continuation character in Python, where otherwise it is whitespace sensitive. When Python detects this, it checks to ensure that the \ is the last character on the line.

Edit: Shifty edit. Naughty lol
Sasarai




PostPosted: Wed Jul 22, 2009 3:05 am   Post subject: Re: RE:String Manipulation Exercise (Generating an nXn box)

[quote="[Gandalf] @ Wed Jul 22, 2009 2:13 am"]As a quick guess; the new line character should always be in "string quotes\n", otherwise \ acts as a line continuation character in Python, where otherwise it is whitespace sensitive. When Python detects this, it checks to ensure that the \ is the last character on the line.

Edit: Shifty edit. Naughty lol[/quote]

XD hehe, I figured you might've gone to bed so I decided to try and look it up. Smile Thank you for reply though Very Happy. Still trying to think on this one. With loops it seems so simple =/.
McKenzie




PostPosted: Wed Jul 22, 2009 7:08 pm   Post subject: Re: String Manipulation Exercise (Generating an nXn box)

Sasarai, don't get too hung up on this one. I'm trying to get you used to playing with strings the same way you play with numbers, in this case adding and multiplying them. I assume that you can get a number from the user and display that many Xes. All you need to do is add the newline onto the end and multiply it again.
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Thu Jul 23, 2009 10:30 am   Post subject: RE:String Manipulation Exercise (Generating an nXn box)

I did it like this:
Python:

number = input ("Enter a number: ")
print (("X" * number)+"\n") * number


Edit: Added syntax tags.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: