user3440123
user3440123

Reputation: 35

Why am I getting "keyword can't be an expression"?

Ok, so I have this as an assignment (I'm in an intro course right now).

Write a function make_x_table (r,c): that creates a multiplication table of r rows and c cols where each entry = r*c and it returns it.

and I wrote this code for it:

def make_x_table (r,c):    
    for rownum in range(1, r+1):
        for colnum in range(1, c+1):
            v = colnum*rownum
            print(str(v) + ' ' + end='')
        print ()

Basically, this is an example of what I want:

make_x_table(3,4)
1 2 3 4
2 4 6 8
3 6 9 12

I keep getting an error saying "keyword can't be an expression" and it'll highlight the parentheses before str(v). I'm not sure why I'm getting this error. Any help?

Upvotes: 1

Views: 1547

Answers (4)

steveha
steveha

Reputation: 76735

I think this is what you want:

print(str(v) + ' ', end='')

You don't use the + operator to specify end, you put it as an argument so it's set off with a comma.

Upvotes: 2

Aaron Hall
Aaron Hall

Reputation: 395633

print(str(v) + ' ' + end='')

Should probably be

print(str(v) + ' ', end='')

Upvotes: 2

Cody Piersall
Cody Piersall

Reputation: 8557

end is a keyword argument to the print function; Python's getting really confused that you're doing

print(str(v) + ' ' + end='')
#                  ^ 

in your print function. You probably wanted to do

print(str(v) + ' ', end='')
#                 ^ 

Upvotes: 2

Elalfer
Elalfer

Reputation: 5338

I suppose print(str(v) + ' ' + end='') should be print(str(v) + ' ' + end + '')

Upvotes: 2

Related Questions