Multiplication Table For Python in IEP? - python

I Am using python 3.3 with IEP and i am trying to make a multiplication table that is nice an orderly. Everywhere i look online says it will be nice but it ends up just being 1 row and long where i want
1 2 3 4
2 4 6 8
3 6 9 12
the code i find is generally like this... SO whats wrong with it?
def main():
i = 1
print("-" * 50)
while i < 11:
n = 1
while n <= 10:
print("%4d" % (i * n),)
n += 1
print("")
i += 1
print("-" * 50)
main()

Because there is a line break after each print
Change 7th line to
print("%4d" % (i * n), end=" ")

The problem is right here:
print("%4d" % (i * n),)
Each print call implicitly puts a line break at the end of the output, but you can change that by providing the end keyword argument to print().
You can do something like this:
In [1]: def print_table(size):
...: for i in range(1, size+1):
...: print(''.join('{:>4d}'.format(i*j) for j in range(1, size+1)))
...:
In [2]: print_table(5)
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Related

Unable to print a range of results with a space between every 3 results

How can I print a range of results with a space between every three results? 
When I print something like the following:
for i in range(1, 11):
print(i)
Output I get:
1
2
3
4
5
6
7
8
9
10
Expected output:
1
2
3
4
5
6
7
8
9
10
for i in range(1, 11):
print(i)
if i % 3 == 0:
print('')
Should do it!
Basically, it just checks that i is modulo 3. If it is, it will just print an empty line.
Example with a more complex loop
In the case that you have a loop that goes from a to b and that you want to print an empty line every c print, you can do:
for i in range(a, b):
print(i)
if (i - a + 1) % c == 0:
print("")

Python string length generate output

I am trying to create ruler by print, it should look like this for input value 5:
Im trying to change in my code numbers to symbols, my code is:
length = str(input("Enter the ruler length = "))
def ruler(string):
top = []
top_out = []
bottom = []
for i in range(length):
top.append((i+1)//10)
bottom.append((i+1)%10)
for i in range(length):
if ((i+1)//10) == 0:
top_out.append(" ")
elif (((i+1)//10) in list(sorted(set(top)))) and (((i+1)//10) not in top_out):
top_out.append(((i+1)//10))
else:
top_out.append(" ")
print (''.join(list(map(str, top_out))))
print (''.join(list(map(str,bottom))))
print (string)
How to correct it to get appropriate output format of a ruler?
Ruler printing can be down by pretty small function like this,
def print_ruler(n):
print('|....'*(n)+'|')
print(''.join(f'{i:<5}' for i in range(n+1)))
Execution:
In [1]: print_ruler(5)
|....|....|....|....|....|
0 1 2 3 4 5
In [2]: print_ruler(10)
|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10
In [3]: print_ruler(15)
|....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
For double-digit numbers, It doesn't come to the center.
For ex: For 12, | align with number 1 or 2 it can't not make into the center of 12

How is print('\r') or print(' ') giving me the output?

We were asked to print the following output:
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6
7 7 7 7
8 8 8
9 9
10
I understand that it would require two loops so I tired this:
a = int(input())
i = a
f = 1
while i>0:
for j in range(i):
print(f,end=' ')
f += 1
i -= 1
print('\r')
With this I am getting the desired output, but as soon as I remove the last line of print('\r') the output becomes something like this:
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 7 7 7 7 8 8 8 9 9 10
The desired output also comes out when I used print(' ') instead of print('\r'), I don't understand why this is happening?
Ps: I am a noob coder, starting my freshman year, so please go easy on me, if the formatting is not up to the mark, or the code looks bulky.
Probably not helping you so much but the following code produces the expected output:
a = 10
for i, j in enumerate(range(a, 0, -1), 1):
print(*[i] * j)
# Output:
1 1 1 1 1 1 1 1 1 1 # i=1, j=10
2 2 2 2 2 2 2 2 2 # i=2, j=9
3 3 3 3 3 3 3 3 # i=3, j=8
4 4 4 4 4 4 4 # i=4, j=7
5 5 5 5 5 5 # i=5, j=6
6 6 6 6 6 # i=6, j=5
7 7 7 7 # i=7, j=4
8 8 8 # i=8, j=3
9 9 # i=9, j=2
10 # i=10, j=1
The two important parameters here are sep (when you print a list) and end as argument of print. Let's try to use it:
a = 10
for i, j in enumerate(range(a, 0, -1), 1):
print(*[i] * j, sep='-', end='\n\n')
# Output:
1-1-1-1-1-1-1-1-1-1
2-2-2-2-2-2-2-2-2
3-3-3-3-3-3-3-3
4-4-4-4-4-4-4
5-5-5-5-5-5
6-6-6-6-6
7-7-7-7
8-8-8
9-9
10
Update
Step by step:
# i=3; j=8
>>> print([i])
[3]
>>> print([i] * j)
[3, 3, 3, 3, 3, 3, 3, 3]
# print takes an arbitrary number of positional arguments.
# So '*' unpack the list as positional arguments (like *args, **kwargs)
# Each one will be printed and separated by sep keyword (default is ' ')
>>> print(*[i] * j)
To make it all easier and prevent errors, you can simply do this:
n = 10
for i in range(1, n + 1):
txt = str(i) + " " # Generate the characters with space between
print(txt * (n + 1 - i)) # Print the characters the inverse amount of times i.e. 1 10, 10 1
Where it generates the text which is simply the number + a space, then prints it out the opposite amount of times, (11 - current number), i.e. 1 ten times, 10 one time.
I suggest using 2 or 4 spaces for indenting. Let's take a look:
a = int(input())
i = a
f = 1
while i>0:
for j in range(i):
print(f,end=' ')
f += 1
i -= 1
print('\r')
Notice the print(f,end=' ') within the inner loop. the end=' ' bit is important because print() appends a trailing new line \n to every call by default. Using end=' ' instead appends a space.
Now take a look at print('\r'). It does not specify end=' ', so it does still append a newline after each call to print. The fact that you additionally print a \r is inconsequential in this case. You could also just do print().
you can do this way :
rows = 10
b = 0
for i in range(rows, 0, -1):
b += 1
for j in range(1, i + 1):
print(b, end=' ')
print('\r')
No need for multiple loops.
for i in range(1,11):
# concatenate number + a space repeatedly, on the same line
# yes, there is an extra space at the end, which you won't see ;-)
print(f"{i} " * (11-i))
output:
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6
7 7 7 7
8 8 8
9 9
10
As to what's happening with your code...
A basic Python print prints on a line, meaning that it ends with a line feed (which moves it to the next line).
So, if I take your word for it, you've done all the hard work of say the first line of 10 ones with spaces, when you are done at the following point.
#your code
f += 1
i -= 1
Now, so far you've avoided that line feed by changing the end parameter to print so that it doesn't end with a newline. So you have:
1 1 1 1 1 1 1 1 1 1
And still no line feed. Great!
But if you now start printing 2 2 2 2 2 2 2 2 2 , it will just get added to... the end of the previous line, without line feed.
So to force a line feed, you *print anything you want, but without the end parameter being set, so that print now ends with the linefeed it uses by default.
Example:
#without line feed
print("1 " * 3, end=' ')
print("2 " * 2, end=' ')
output:
1 1 1 2 2
Lets try printing something, anything, without a end = ' ')
print("1 " * 3, end=' ')
#now add a line by a print that is NOT using `end = ' '`
print("!")
print("2 " * 2, end=' ')
output:
1 1 1 !
2 2
OK, so now we have a line feed after ! so you jump to the next line when printing the 2s. But you don't want to see anything after the 1s.
Simples, print anything that is invisible.
print("1 " * 3, end=' ')
#now add a line by a print, but using a non-visible character.
#or an empty string. Tabs, spaces, etc... they will all work
print(" ")
print("2 " * 2, end=' ')
output:
1 1 1
2 2
This would also work:
print("1 " * 3, end=' ')
#we could also print a linefeed and end without one...
print("\n", end="")
print("2 " * 2, end=' ')

Python: How to print multiplication table based on two inputs?

I'm attempting to write a program called multChart(x,y) that prints a multiplication table based on two inputs, one specifying the number of rows to print and another specifying the number of columns. So it would look like this:
>>> multChart(4,5):
1: 1 2 3 4 5
2: 2 4 6 8 10
3: 3 6 9 12 15
4: 4 8 12 16 20
Here's what my current code looks like:
def multChart(x,y):
for i in range(1,x+1):
print(i,':',i*1,i*2,i*3,i*4,i*5)
I'm totally stuck on how to implement the y value. I also know there should be a better way of printing the multiplication instead of i * multiples of five, but I'm not sure what loop to use. Any help would be greatly appreciated.
You need another loop inside your print for looping over the y range:
def multChart(x, y):
for i in range(1, x+1):
print(i, ':', *[i * z for z in range(1, y+1)])
def multChart(x,y):
for i in range(1,x+1):
print(i, ':', end=" ")
for j in range(1,y+1):
print(i*j, end =" ")
print()
multChart(4,5)
produces
1 : 1 2 3 4 5
2 : 2 4 6 8 10
3 : 3 6 9 12 15
4 : 4 8 12 16 20
You can use a second for loop for the second index. Also, note that you can use end in the print statement.
def multChart(x,y):
for i in range(1,x+1):
print(i,':',*list(map(lambda y: i*y,list(range(1,y+1 ) ) ) ) )
multChart(4,5)

Multiplication Tables in Python

this is my code right now:
loop_count = 1
for i in range(mystery_int):
for x in range(1,mystery_int):
print(x*loop_count, end=" ")
print (loop_count)
loop_count+=1
this is what it is supposed to print:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
But it prints:
1 2 3 4 1
2 4 6 8 2
3 6 9 12 3
4 8 12 16 4
5 10 15 20 5
You need to range till mystery_int + 1 because in range, second argument is exclusive. So, for example, range(1,6) gives numbers from 1 to 5.
Also, I added an empty print() which basically adds a newline to match with desired output.
Using end='\t' further aligns output properly.
loop_count = 1
mystery_int = 5
for i in range(mystery_int):
for x in range(1, mystery_int + 1):
print(x * loop_count, end='\t')
print()
loop_count += 1
the range for x should be range(1,mystery_int+1), and you also incorrectly print loop_count at the end of each line (which I replaced with the empty string, just to produce a newline).
loop_count = 1
for i in range(mystery_int):
for x in range(1,mystery_int+1):
print(x*loop_count, end=" ")
print('')
loop_count+=1
Note that the loop_count variable is not really needed. You could write the program as:
for i in range(1,mystery_int+1):
for x in range(1,mystery_int+1):
print(x*i, end=" ")
print('')
or even better as:
for i in range(1,mystery_int+1):
print(*[x*i for x in range(1,mystery_int+1)], sep=" ")
you are running on two for loops in addition to using another counter, i would recommend sticking only to the loops:
for i in range(1,mystery_int+1):
for x in range(1,mystery_int+1):
print(i*x, end=" ")
print("") # new line

Categories