I want to create a function that takes 2 parameter, and prints the multiplication table for this number in a nice format where rows are separated by lines. This is the target:
target design
I have tried, but have no idea where to integrate the "--------" string. Any ideas?
def multi_table(x,y):
for row in range(1, x+1):
for col in range(1, y+1):
num = row * col
if num < 10: blank = ' '
else:
if num < 100: blank = ' '
print(blank, num, end = '')
print()
multi_table(4,5)
You need to add the print statement between the row and column loop. You also need to ensure that you end the print statement with a new line character \n. Refer below.
def multi_table(x,y):
for row in range(1, x+1):
print("---------------------\n")
for col in range(1, y+1):
num = row * col
if num < 10: blank = ' '
else:
if num < 100: blank = ' '
print(blank, num, end = '')
print()
multi_table(4,5)
The print() is used to go to the next line, and that's where you want to add the "---------------". So change the print() to print('\n------------------------\n'). \n indicates to go to the next line.
To compensate for y, you can use the following,
also, you can simplify the formatting with the format string method:
def multi_table(x,y):
for row in range(1, x+1):
print('----' * y)
for col in range(1, y+1):
num = row * col
print('{:4}'.format(num), end = '')
print()
Related
for i in range(n):
for j in range(n):
z = i * n + j
if z < 10:
print(f" {z}", end = " ")
else:
print(f"{z}" , end = " ")
print()
I have tried the end= " " method but I keep getting the whitespace error, I am a beginner thus I am unable to implement other methods to address the same. The image embedded below has the desired output
[The output required ][1]
[1]: https://i.stack.imgur.com/StAVT.png
This approach creates the full matrix as a string s first and then only prints once in the end:
s = ''
for i in range(n):
for j in range(n):
z = i * n + j
s += ' '
if z < 10:
s += ' '
s += str(z)
s += '\n'
print(s)
Why does my code not work in the 2nd iteration? I want to give input in row = 3 or 5 or x, and then I expect it to produce the following output pattern:
#When row = 3
1|. . #
2|. ##
3|###
Please note that the number of "." will be opposite to the number of "#"
The i loop prints a new line, the j loop prints "#", and the k loop prints "."
row = 3
for i in range(1, row + 1):
for j in range(1, i + 1):
for k in range(1, (row - i) + 1):
print(".", end= "")
print("#", end="")
print("")
Your k loop runs too many times to do what you want after the first iteration.
However I think this does what you want, hope it helps:
row = 3
n_hash = 0
n_dots = row
for i in range(row):
print(f'{i+1}|{n_dots * "."}{n_hash * "#"}')
n_dots -= 1
n_hash += 1
Given a string, print its even-indexed and odd-indexed characters as
space-separated strings on a single line.
Example:
s = adbecf => Print abc def
My approach:
t = input()
p = len(t)
for i in range(p):
s = t[i]
n = len(s)
even = []
odd = []
for j in range(n):
if j % 2 == 0:
even.append(s[j])
for j in range(n):
if j % 2 != 0:
odd.append(s[j])
first_join=''.join(even)
second_join = ''.join(odd)
print("{} {}".format(first_join,second_join)
No need to use complex ways. here is a easy way to do it.
t = input()
p = len(t)
ans = "" # create empty string
for i in range(0,p,2):
ans += t[i] # Add even characters
ans += " " # Add space.
for j in range(1,p,2):
ans += t[j] # Add odd characters
print(ans)
Input: adbecf
Output: abc def
How to print the pattern like this:
(need to print n-1 lines)
input=3
----#
--#-#-#
input=6
----------#
--------#-#-#
------#---#---#
----#-----#-----#
--#-------#-------#
My code:
row = int(input())
for i in range(1, row):
for j in range(1,row-i+1):
print("-", end="")
for j in range(1, 2*i):
if j==1 or j==2*i-1:
print("#", end="")
else:
print("-", end="")
print()
MY OUTPUT:
input=5
----#
---#-#
--#---#
-#-----#
Please explain how to do??
There are a few things missing and to be improved in your code:
There's no need to make a loop to print the same character again and again: on python you can use the product to repeat the character an x number of times. For example: "-" * 3 == "---"
They way you calculate the hyphens in the middle is fine, but you need to do it twice and add an "#" in between.
You can build the strings part by part first and then print the whole line, avoiding having to print an empty line in the end of the loop.
Personally, since the first line is going to have one "#" and not three, I prefer to calculate it and print it separately.
With these improvements, a solution to your problem could be:
row = int(input())
print("-" * (row - 1) * 2 + "#")
for i in range(row - 2, 0, -1):
left_hyphens = "-" * i * 2
mid_hyphens = "-" * (1 + 2 * (row - 2 - i))
print(left_hyphens + "#" + mid_hyphens + "#" + mid_hyphens + "#")
row = int(input())
for i in range(1, row):
for j in range(1,2*(row-i)+1):
print("-", end="")
for j in range(1, 4*i):
if j==1 or j==2*i-1 or j==4*i-3:
print("#", end="")
elif j<=4*i-3:
print("-", end="")
print()
My current code is under here with the pattern I want to be displayed
XOOOOOX
OXOOOXO
OOXOXOO
OOOXOOO
OOXOXOO
OXOOOXO
XOOOOOX
Code:
#starting from the 1st row and ending at the 8th row
for row in range (1, 8):
#within each row, starting from the 1st col and ending in the 8th col
for col in range(1, 8):
#decide what to print at the current location
if ((row - col)) == 0:
print("X", end="")
elif((row + 1)) == 0:
print("X", end="")
else:
print("O", end="")
#go onto the next row
print()
If you want to display x, the following code is sufficient:
You can differ the size of letter by N, try to avoid hard-coding some arbitrally numbers.
N = 8
# starting from the 1st row and ending at the 8th row
for row in range(1, N):
# within each row, starting from the 1st col and ending in the 8th col
for col in range(1, N):
# decide what to print at the current location
if row == col or row == N-col:
print("X", end="")
else:
print("O", end="")
# go onto the next row
print()
Your logic does nothing to insert the second X, as you've seen.
for row in range (1, 8):
...
elif((row + 1)) == 0:
Since row takes on values in the range 0-7, this isn't possible: it can be true only when row = -1. Yes, Python allows you to index a list from the right, but a variable used as an index does not automatically take on a second value to satisfy those semantics. You have to explicitly give it a single value.
You need to rewrite your condition to compare row to the end index, and you'll have to involve col, just as you did with the main diagonal.
You can convert your string to list and make the change symmetrical based on the length of your string and the current i
LEN = 7#length of string
string = ""
#initialize string
for i in range(LEN):
string += "O"
#change and print
for i in range(LEN):
new_string = list(string)#from string to list
new_string[i] = "X"#change the i
new_string[LEN-i-1] = "X"#symmetrical change
new_string = "".join(new_string)#list to string
print(new_string)
You can change the LEN and the characters as you wish