+ 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.
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
+ 4
What kind of input are you working with and what kind of array are you supposed to create? Numpy array, list of lists, ...
+ 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.
+ 3
Ipang
Very creative way to calculate using the row and the column 👍
+ 3
Thank you Terel Schmitt 🙏
+ 3
thank you everybody for your time. It means a lot to me❤️
+ 2
2 for loop?
+ 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
+ 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?
+ 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
+ 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
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)
0
that's true, what should I put in the logic?
0
You can search this on Google
- 1
Having difficulty with the project work on machine learning.Can anyone be of help please?
- 5
This is car game with the help of html and Css . Hope you like it :-
https://code.sololearn.com/Wam3dPJKIBTU/?ref=app 📝👨🏻💻