Reputation: 3
This is my code for a times table:
Y=int(input(""))
X=int(input(""))
for i in range(1,Y+1):
print("")
for j in range(1,X+1):
print("{:>4}".format(str(i*j)),end="")
This is the output when X=3 and Y=2:
1 2
2 4
3 6
The output is correct but there is an extra spacing at the top that i need to get rid of.
1 2
2 4
3 6
This is what i would like to get. Can anyone please help with this problem.
Upvotes: 0
Views: 95
Reputation: 54168
You need to move the empty print
at the end, and no need of ""
into it, also no need to cast as str
for i in range(1,Y+1):
for j in range(1,X+1):
print("{:>4}".format(i*j),end="")
print()
You can also concat the inner loop, like
for i in range(1,Y+1):
val = ""
for j in range(1,X+1):
val+= "{:>4}".format(i*j)
print(val)
Or shorter
for i in range(1, Y + 1):
print("".join(map(lambda j: "{:>4}".format(i * j), range(1, X + 1))))
Upvotes: 1
Reputation: 1518
for i in range(1,Y+1):
for j in range(1,X+1):
print("{:>4}".format(str(i*j)),end="")
print("")
Upvotes: 0
Reputation: 12241
Move the print("")
line to the bottom, but keep the same indentation:
y = int(input("y: "))
x = int(input("x: "))
for i in range(1, y+1):
for j in range(1, x+1):
print("{:>4}".format(i*j), end="")
print()
Upvotes: 0
Reputation: 1767
Try this below:
Y = int(input(""))
X = int(input(""))
for i in range(1, Y + 1):
if i > 1:
print()
for j in range(1, X + 1):
print("{:>4}".format(str(i * j)), end="")
OR
Y = int(input(""))
X = int(input(""))
for i in range(1, Y + 1):
for j in range(1, X + 1):
print("{:>4}".format(str(i * j)), end="")
print()
Upvotes: 0