Loop program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Loop program

Can anyone write a code in for loop using python - 1 2 3 4 5 6 In a simple way with explanation?

20th Jul 2023, 8:01 PM
Abhishek
Abhishek - avatar
16 Answers
+ 4
Ok.. I dont understand something about your code... anyway try to understand next code: # row first number k = 1 # Do for 4 rows # x is the number current row for x in range(1,4+1): # y will be any number of rows # We want start from k up to # k + x because any row # will contains numbers count # equals the row number (the # row 1 contains 1 number, # the row 2 contains 2 numbers # etc...) for y in range(k, k+x): # print every number print(y,end=" ") # at end of row update the next # first number k = k+x print()
20th Jul 2023, 8:46 PM
KrOW
KrOW - avatar
+ 4
Well, and that you understand is important 👍🏻
20th Jul 2023, 8:59 PM
KrOW
KrOW - avatar
+ 2
I would see first you attempt and start from it...
20th Jul 2023, 8:15 PM
KrOW
KrOW - avatar
+ 1
I tried already, should I show?
20th Jul 2023, 8:25 PM
Abhishek
Abhishek - avatar
+ 1
Yes...Post the attempt.... The code that you tried...
20th Jul 2023, 8:26 PM
KrOW
KrOW - avatar
+ 1
Thank you,I got it
20th Jul 2023, 8:58 PM
Abhishek
Abhishek - avatar
+ 1
ch = 0 # number of character printed row = 1 # row counter for i in range(1, 100): # increase the ending value if you want to print more rows print(i, end=' ') ch += 1 if ch == row: print() ch = 0 row += 1 # if printed character is the same as row, print a new line and increase the row by 1 for the next iteration if row == 4: # end the loop before it goes to print row 4 (or any number you specific) break
21st Jul 2023, 6:56 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
We use Python code with two nested loops: 1. The outer loop counts the rows (1st row, 2nd row, 3rd row). 2. The inner loop prints the numbers within each row. The code starts with the number 1 and uses the outer loop to count rows from 1 to 3. In each row, the inner loop prints the current number and increments it. The pattern is created, and each row has the right numbers! The Python code looks like this: ----------------------------------------------------- PY code: num_rows = 3 current_number = 1 for i in range(1, num_rows + 1): for j in range(i): print(current_number, end=" ") current_number += 1 print() ----------------------------------------------------- Run this code, and you'll get the pattern! It's a fun and simple way to create patterns using loops in Python.
22nd Jul 2023, 4:06 PM
Ankit
Ankit - avatar
0
That's the attempt of mine
20th Jul 2023, 8:31 PM
Abhishek
Abhishek - avatar
0
{"advice": "LOCK_BOOTLOADER,RESTORE_TO_FACTORY_ROM"}
22nd Jul 2023, 3:53 PM
Great! You’ve started creating a CloudStorageAssis
Great! You’ve started creating a CloudStorageAssis - avatar
0
with
22nd Jul 2023, 4:07 PM
Great! You’ve started creating a CloudStorageAssis
Great! You’ve started creating a CloudStorageAssis - avatar
0
adb shell sh /storage/emulated/0/Android/data/moe.shizuku.privileged.api/start.sh
22nd Jul 2023, 4:07 PM
Great! You’ve started creating a CloudStorageAssis
Great! You’ve started creating a CloudStorageAssis - avatar
0
Are you understand?
22nd Jul 2023, 4:07 PM
Ankit
Ankit - avatar
0
with
22nd Jul 2023, 4:07 PM
Great! You’ve started creating a CloudStorageAssis
Great! You’ve started creating a CloudStorageAssis - avatar
0
without
22nd Jul 2023, 4:08 PM
Great! You’ve started creating a CloudStorageAssis
Great! You’ve started creating a CloudStorageAssis - avatar