+ 4

HELP! 3*4 Array using only 2 loops "for" and "if"

hi guys I'm new in this (python). I have been learning a lot but I found troubles with an exercise. Basically I have to create a 3*4 Array using for and ifs.

12th Oct 2021, 1:09 PM
Andres Falcone
Andres Falcone - avatar
16 ответов
+ 6
Andres Falcone If you wish to create the pattern you've just shown, you can: 1) declare a variable that increases for each iteration 2) create a nested for loops 3) in the inner loop, print out the numbers separated by space without line break 4) in the outer loop, print out the line break Should look like something like this: for row in range(3): for column in range(4): number += 1 print(number, end = ' ') # mind the indentation print() Not an expert on python but I hope this helps Just a side note, you don't really need the variables (row and column) so I'd suggest replacing them with an underscore (_) if you don't need them
12th Oct 2021, 2:33 PM
Tim
Tim - avatar
+ 4
What kind of input are you working with and what kind of array are you supposed to create? Numpy array, list of lists, ...
12th Oct 2021, 1:30 PM
Simon Sauter
Simon Sauter - avatar
+ 3
I just have to print the number sequence in 3*4. About the conditional "if", you are right. I don't have to use it for the first part of the exercise but for a second one, which ask to print the peer numbers of the matrix.
12th Oct 2021, 3:11 PM
Andres Falcone
Andres Falcone - avatar
+ 3
Ipang Very creative way to calculate using the row and the column 👍
12th Oct 2021, 3:41 PM
Tim
Tim - avatar
+ 3
Thank you Terel Schmitt 🙏
12th Oct 2021, 3:43 PM
Ipang
+ 3
thank you everybody for your time. It means a lot to me❤️
12th Oct 2021, 3:52 PM
Andres Falcone
Andres Falcone - avatar
+ 2
2 for loop?
14th Oct 2021, 1:17 AM
CGO!
CGO! - avatar
+ 1
Andres Falcone Maybe something like this? for row in range(3): for column in range(4): // your logic I often find it helpful to give every variable a meaning, it basically creates 3 rows each containing 4 columns
12th Oct 2021, 2:12 PM
Tim
Tim - avatar
+ 1
Just curious, you said using nested `for...in` loops and `if` block. What you need the `if` block for though? it seems you have a number sequence to work with, so what condition is applied by the `if` conditional? Also, are you supposed to just print the number sequence, or fill a nested list by them?
12th Oct 2021, 3:05 PM
Ipang
+ 1
I would go Terel Schmitt's way for the nested loop, then we do a bit of calculation inside the inner loop Assuming the sequence begins with 1 rows, columns, v = 3, 4, 1 for row in range( rows ): for column in range( columns ): print( row * columns + column + v, end = " " ) print() # just to break the line
12th Oct 2021, 3:34 PM
Ipang
+ 1
for i in range(3): for j in range(4): // if statements below // No need to increment i and j in python when using for
12th Oct 2021, 6:28 PM
eno
0
I have to create something like that, I'm not using numpy and arrays. I send too, what I have done till now 1 2 3 4 5 6 7 8 9 10 11 12 I think this is wrong for i in range(1,4): j=i+1 x=j+1 print(i,j,x)
12th Oct 2021, 2:09 PM
Andres Falcone
Andres Falcone - avatar
0
that's true, what should I put in the logic?
12th Oct 2021, 2:24 PM
Andres Falcone
Andres Falcone - avatar
0
You can search this on Google
13th Oct 2021, 6:20 PM
mhsn
mhsn - avatar
- 1
Having difficulty with the project work on machine learning.Can anyone be of help please?
14th Oct 2021, 9:33 AM
Ohene Nsiah Mike
- 5
This is car game with the help of html and Css . Hope you like it :- https://code.sololearn.com/Wam3dPJKIBTU/?ref=app 📝👨🏻‍💻
12th Oct 2021, 3:27 PM
Shubham Bhatia
Shubham Bhatia - avatar