when I try to run this c开发者_如何学Goode :
def table():
nb = 7
i = 0
while i < 20 :
if ((i+1)*nb)%3 == 0 :
print(i+1 , "*" , nb , "=" , (i+1)*nb'*')
else :
print(i+1 , "*" , nb , "=" , (i+1)*nb)
i += 1
I get an syntax error for this part (the last ')
print(i+1 , "*" , nb , "=" , (i+1)*nb'*')
And I really don't understant why, is there any issue ?
I suspect you're trying to do:
print(i+1 , "*" , nb , "=" , (i+1)*nb, '*')
^
Note the comma before the final argument.
You are missing a comma, and also your indentation was all wrong:
def table():
nb = 7
i = 0
while i<20 :
if ((i+1)*nb)%3==0 :
print(i+1 , "*" , nb , "=" , (i+1)*nb, '*')
else :
print(i+1 , "*" , nb , "=" , (i+1)*nb)
i +=1
You are missing a comma. Try this:
print(i+1 , "*" , nb , "=" , (i+1)*nb , '*')
Your syntax error is here : print(i+1 , "*" , nb , "=" , (i+1)*nb'*')
in the (i+1)*nb'*'
.......the nb and ' are not seperated.
print(i+1 , "*" , nb , "=" , (i+1)*nb, '*')
should be right
You are doing nb'*'
, which is equivalent to 7'*'. There is no int/str juxtaposition operator, I guess you want:
print(i+1 , "*" , nb , "=" , (i+1)*nb*'*')
but you really aren't clear in this programme. I would probably use something like:
def table(multiplier=7, maximum=20):
for i in range(1, maximum+1):
answer = i * multiplier
if (answer) % 3 == 0:
print("%d * %d = %s" % (i, multiplier, '*' * answer))
else :
print("%d * %d = %d" % (i, multiplier, answer))
as it's got clear variable names, allows you to pass in specific multipliers and uses a more pythonic loop.
精彩评论