1

Im trying to get an exercise from a python 2 book to work in python 3.

1 def printMultiples(n):
2    i = 1
3    while i <= 6:
4      print(n*i, '/t',)
5      i = i + 1
6    print()

My problem is line 5. I understand that in python2 print is a statement and not a function. Just adding the parenthesis doesn't work. It just doesn't recognize the string, '/w'. I just cant figure out how to make this work in a function. I tried using str(n*i) to convert the integer to a string but that threw an error.

I know that you python guys know what I'm trying for and how to do it.

2
  • As a side note, your whole loop is overly complicated. If you want to loop over the numbers from 1 to 6 inclusive, just do for i in range(1, 7):. No need to pre-initialize it to a start value, check it against an end value, and increment it manually (which gives you three separate places where a silly typo can lead to a hard-to-track-down bug, instead of just one). Commented Jan 15, 2014 at 1:45
  • 1
    I assume this is from a intro book and the gentle approach to get the basics across before delving into the pythonic way of doing things. While your one liner example is a beautiful example of what can be done in Python it could be a little hard to digest for a programmer learning the language or indeed new to programming. Commented Jan 15, 2014 at 12:16

2 Answers 2

4

I assume you are trying to print a tab separated list of values, to change the code to work in Python 3 use the following function:

def printMultiples(n):
    i = 1
    while i <= 6:
        print(n*i, end='\t')
        i = i + 1
    print()

The print statement in Python 3 has the following signature:

print(*args, sep=' ', end='\n', file=None)

To change the end of line character in Python 3 similar to putting a comma at the end of the print statement in previous versions of Python is to use the end keyword.

Sign up to request clarification or add additional context in comments.

3 Comments

It's worth noting that, while the way the print function works might initially seem cumbersome by comparison to Python 2, this means that the entire function can be written using a list comprehension and a single print statement: printMultiples = lambda n: print( *[n*i for i in range(1,7)], sep='\t' )
@cge that is a good point, the use of list comprehensions (or a generator expression) in this case does shrink the method down to a one liner really demonstrates the power of python constructs. For a person new to python however I think the more verbose version makes it a little more clear.
In Python 2.x, he was printing two values: n*i and '\t', with no ending (or, rather, a magic-space ending). Your Python 3.x code is printing one value, n*i, with '\t' as an ending. A more direct translation would be print(n*i, '\t', end=''). Of course that does exactly the same thing, and is longer, so I'm not suggesting you change your code, just that it might be worth explaining the difference (especially since your answer gets into ways to use the end argument).
0

I'm not sure what do you want from the last line print()

def printMultiples(n):
    for i in range(1,7):
        print(n*i, '\t')

Does this work for you?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.