Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

Hello, I'm having some trouble with this encapsulation. The textbook "How to Think Like a Computer Scientist" tells me to input this in: def printMultiples(n): i=1 while i <= 6: print n*i, ’\t’, i=i+1 print() It says that when this is put in, if n=3, it should look like this. 3 6 9 12 15 18 However, I do not seem to be able to get the numbers to go side ways. They are always printed vertically: 3 6 9 12 15 18 What should I do? Thank you.

OpenStudy (anonymous):

\t means tab

OpenStudy (anonymous):

you should get two numbers number1 number2 number1 number2 and etc. like that

OpenStudy (maitre_kaio):

It would be easier to help if you had provided code in a pastebin, and the whole code. Obviously, the code that you posted is not the code you ran, because it has errors.

OpenStudy (anonymous):

and what version you using? is it python3?

OpenStudy (maitre_kaio):

http://codepad.org/wRcZjG5a

OpenStudy (anonymous):

i thought he want to print that i+1 too

OpenStudy (anonymous):

I use 3.2.2

OpenStudy (anonymous):

I'm aware that \t is tab, but it just doesn't work....Can I take a screen shot to show you?

OpenStudy (anonymous):

/Users/student/Desktop/Screen Shot 2012-01-27 at 8.34.44 PM.png

OpenStudy (anonymous):

it's not screenshot lol

OpenStudy (anonymous):

ugh...it doesn't go on properly >>> def printMultiples(n): i=1 while i<=6: print (n*i, '\t'), i=i+1 print() >>> print Multiples(1) SyntaxError: invalid syntax >>> printMultiples(1) 1 2 3 4 5 6 >>> def pMultiples(n): i=1 while i<=6: printMultiples(i) i=i+1 >>> pMultiples(1) 1 2 3 4 5 6 2 4 6 8 10 12 3 6 9 12 15 18 4 8 12 16 20 24 5 10 15 20 25 30 6 12 18 24 30 36

OpenStudy (anonymous):

That's the part I'm struggling with.

OpenStudy (anonymous):

click print screen, go to paint, click ctrl+v, save file, attach file

OpenStudy (anonymous):

you didn't answer about your python version either

OpenStudy (anonymous):

oh, sorry, it's 3.2.2

OpenStudy (anonymous):

Python 3.2.2 it says

OpenStudy (anonymous):

so it's a bit different, this course uses second version

OpenStudy (anonymous):

0.0 oh...if it were just 3.2 would it work? Is there a way to get around this?

OpenStudy (anonymous):

>>> print Multiples(1) SyntaxError: invalid syntax you get error because you don't have such function called Multiples

OpenStudy (anonymous):

yeah...I figured that part out....I just don't get why the printMultiples(1) after that doesn't give a horizontal listing of the elements.

OpenStudy (anonymous):

print (n*i, '\t'), probably means that it print n*i makes tab and goes to new line

OpenStudy (anonymous):

mhmm, the text mentioned something previously about how print makes it go to the next line...and because its looped, it keeps changing the line every time.

OpenStudy (anonymous):

well as i said after print(blablababla) it always goes to new line

OpenStudy (anonymous):

what should I use instead?

OpenStudy (anonymous):

Yes, maitre_kaio, that's what I'm trying to get to, didn't mean to ignore you:( sorry

OpenStudy (anonymous):

im not sure, you should find it in book lol

OpenStudy (anonymous):

ok, thnx!

OpenStudy (anonymous):

maybe: print(n * i, end = '\t') http://docs.python.org/py3k/library/functions.html#print

OpenStudy (anonymous):

hmmm, maybe print(n * i, *, end = '\t') there is that funny asterisk in the docs

OpenStudy (anonymous):

I'll try that out! thnx!

OpenStudy (anonymous):

YOU are AMAZING! it worked ! thanks you so much! I am your fan :)

OpenStudy (anonymous):

which one worked? with or without the asterisk?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!