so basically, I want to make an automatic matrix input and generate the output but the second part of the input shows up with the same output as the first one. is there any wrong logic behind my code? here's my code (im sorry for bad english):
#input menu 2 :
def prgmatriks2() :
ct = 2
for a in range(ct):
m = int (input("input row "+str (a)+": "))
n = int (input("input column"+str (a)+": "))
# initialization matriks
matrix = []
print("Matriks input: ")
#Input :
for x in range(ct) :
for i in range(m): #rows loop
a =[]
for j in range(n): #column loop
a.append(int(input()))
matrix.append(a)
# Print Matriks
for x in range(ct) :
for i in range(m):
for j in range(n):
print(matrix[i][j], end = " ")
print()
and for the output:
output
text version:
Pilihan menu(1-2):
2
input row 0: 2
input column0: 2
input row 1: 2
input column1: 2
Matriks input:
3
2
4
1
6
5
2
1
3 2
4 1
3 2
4 1
instead generate :
3 2
4 1
6 5
2 1
the program repeat the first output :
3 2
4 1
3 2
4 1
print(matrix[i][j], end=" ") replace here i with i+x*n.
So logic for printing Matrices will be like this code :
# Print Matrices
for x in range(ct):
for i in range(m):
for j in range(n):
print(matrix[x*n+i][j], end=" ")
print()
print()
print(matrix)
OUTPUT :
3 2
4 1
6 5
2 1
Related
I would like to print the following pattern in Python
input: 5
output:
5
456
34567
2345678
123456789
I have used the following code but it is not showing the above pattern. Anyone help me on this topic, please?
CODE:
rows = int(input("Enter number of rows: "))
k = 0
count=0
count1=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
while k!=((2*i)-1):
if count<=rows-1:
print(i+k, end=" ")
count+=1
else:
count1+=1
print(i+k-(2*count1), end=" ")
k += 1
count1 = count = k = 0
print()
OUTPUT:
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
If I understand your question correctly, you just want a pattern starting from n and going to 1 in decreasing order left side, and starting from n and going to 2n-1 in increasing order right side
def pattern(n):
for i in range(n,0,-1):
for j in range(1,i):
print(" ",end="")
for k in range(i,2*n-i+1):
print(k,end="")
print()
pattern(5)
5
456
34567
2345678
123456789
So I've recently started learning python and my friend gave me a changllege. How to make a number triangle. So basically, I need to make python let me input a number, for example 5. After I do that, I want python to print
54321
4321
321
21
1
But because I am new to python, I don't know how to do it.
So far, I've got this:
x = int(input('Enter a Number: '))
for i in range(x,0,-1):
print(i,end='')
if i != 0:
continue
else:
break
And the output is:
Enter a Number: 5
54321
Any ideas how to make it print
54321
4321
321
21
1
?
Thanks for any advice
Here is a sample code for you.
Short version (suggest learning about list comprehension and join functions):
x = int(input('Enter a Number: '))
for i in range(x, 0, -1):
print(''.join([str(j) for j in range(i, 0, -1)]))
Longer version which is easier to understand:
for i in range(x, 0, -1):
s = ''
for j in range(i, 0, -1):
s += str(j)
print(s)
Output:
54321
4321
321
21
1
rows = int(input("Inverted Right Triangle Numbers = "))
print("Inverted")
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end = ' ')
print()
Output
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
def trig_print(number):
if number > 0:
for i in range(number):
print(number - i,end='')
print('')
number = number - 1
trig_print(number)
else:
return 0
trig_print(5)
I used to solve it by while loop.
3 step
input initial number.
make list one to initial number.
reverse list and print it.
>>> x = int(input('Enter a Number: '))
Enter a Number: 5
>>> while x >0 :
l=list(range(1,x+1)) # range function
l.reverse() # list.reverse function
print(' '.join(map(str,l))) # join and map function
# I recomanded input space between number for readability.
x -= 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
I want to write a Program for the Fibonacci series and need to show series in Pyramid.
Enter (through command line) the number of times the Fibonacci has to iterate.
Ex:
Enter the number of times
6
Fibonacci Series of the number is: 0 1 1 2 3
Below is the expected output -
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
I have tried this code as below -
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(b,end="")
for j in range(a,i-1):
c=a+b
print(c,end="")
a=b
b=c
print()
But this is giving below output
1
1
11
112
1123
11235
the above output starting from "1" but the expected output should start from "0"
Please help me with correct python code as expected
Thanks
Try:
n = int(input("Enter the number of rows: "))
fib = []
for i in range(n):
fib.append(fib[-2] + fib[-1] if i > 1 else i)
print(' '.join(str(x) for x in fib))
Output:
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
In your code, you are computing the Fibonacci sequence from zero again and again for each row, which is redundant. Instead, you can make a list and add one item at each iteration.
I've used join to insert a blank between entries, which I believe is more "pythonic."
It often makes for simpler code if you separate computation from doing pretty output. So construct your series first:
>>> series = [0,1]
>>> n = 6
>>> while len(series) < n:
series.append(series[-1]+series[-2])
>>> series
[0, 1, 1, 2, 3, 5]
Then do the output:
>>> sseries = [str(s) for s in series]
>>> sseries
['0', '1', '1', '2', '3', '5']
>>> for row in range(len(sseries)+1):
print (" ".join(sseries[:row]))
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
There is some small change that your code will require. You have to make end=" ", so that it will print space there.
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(a, b,end=" ")
for j in range(a,i-1):
c=a+b
print(c,end=" ")
a=b
b=c
print()
I'm using the following code:
for x in range(8):
print(x,end=" ") #Loop run for 8 times(from 0 to 7)
print('\n')
for x in range(1,8):
print(x,end=" ")
When I run it, the output is:
0 1 2 3 4 5 6 7
1 2 3 4 5 6 7
I only need one new line in the answer; that is, I want the output as:
0 1 2 3 4 5 6 7
1 2 3 4 5 6 7
The print function has a default end of a linefeed. If you tell it to print a linefeed, it will do that, and then add the default ending linefeed, resulting in two linefeeds. If you only want one, call it with no arguments:
print()
for x in range(8):
print(x, end=" ")
print()
for x in range(1,8):
print(x, end=" ")
Add another print statement between the loops:
print()
This will print the new line.
In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
This is what I came up with and I have tried many different ways to get the same result but it didn't work.
num=int(input("Enter a number between 1 and 10: "))
rows=int(input("Enter how many rows to of numbers: "))
for i in range(num):
print(i,end=" ")
for x in range(rows):
print (x)
This is the output I came up with:
Enter a number between 1 and 10: 6
Enter how many rows to of numbers: 4
0 1 2 3 4 5 0
1
2
3
You may simply do it like:
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
Output:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Explanation: When you multiply a str with some number say n, new string is returned with original string repeated n times. Doing this you will eliminate your nested loop
Simple solution: Just use nested for loops:
num = int(input("Enter a number between 1 and 10: "))
rows = int(input("Enter how many rows to of numbers: "))
for i in range(num):
print ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()