Jump to content

Python nested loops

Johanes
a=int(8)
count=0

for g in range(a):
 	while a>4:
 		a-=1
 		count+=1
 		print('*'*count)
 	count-=1
 	print('*'*count)

Can someone please help me simplify this? Also if possible help me with an explination the steps ur taking

Link to comment
Share on other sites

Link to post
Share on other sites

What's that supposed to do?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Sauron said:

What's that supposed to do?

*

**

***

****

***

**

*

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

 

6 minutes ago, vorticalbox said:

*

**

***

****

***

**

*

 

Yeah basically its an activity given to us explaining how nested looping works. This is my iterpritation, im asking for feedback on how to simplify my code

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, Johanes said:

 

Yeah basically its an activity given to us explaining how nested looping works. This is my iterpritation, im asking for feedback on how to simplify my code

you can actually do this without the second loop

 

a=int(8)
for g in range(1,a):
  # if the current interator is more than 4
  # Then print a - g
  # example:
  # if g is 5 then example 8 - 5 = 3
  if g > 4:
    print('*'*(a-g))
  else:
    print('*'*g)

A bubble sort is probably a better example

 

arr = [5,1,3,7]
print(arr)
for i in range(len(arr)):
  for j in range(len(arr)-1):
    if arr[j] > arr[j+1]:
      tmp = arr[j+1]
      arr[j+1]=arr[j]
      arr[j]=tmp
print(arr)


[5, 1, 3, 7]
[1, 3, 5, 7]

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, vorticalbox said:

*

**

***

****

***

**

*

 

I was more interested in the exercise description :P

33 minutes ago, Johanes said:

Yeah basically its an activity given to us explaining how nested looping works. This is my iterpritation, im asking for feedback on how to simplify my code

This works:

for i in range(7):
  print("*" * (4 - abs(i - 3)))

It uses the absolute value function to subtract the correct amount from 4 each iteration.

 

But if the exercise specifically asks you to use nested loops then something like this could be better:

for i in range(7):
  for j in range(4 - abs(i - 3)):
    print("*", end="")
  print("")

it's exactly the same but instead of abusing the print function it simply prints "*" j times in a loop.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

finally at my room.. gonna study all em codes xD thanks for all the feedback ^_^

Link to comment
Share on other sites

Link to post
Share on other sites

55 minutes ago, Sauron said:

I was more interested in the exercise description :P

This works:


for i in range(7):
  print("*" * (4 - abs(i - 3)))

It uses the absolute value function to subtract the correct amount from 4 each iteration.

 

But if the exercise specifically asks you to use nested loops then something like this could be better:


for i in range(7):
  for j in range(4 - abs(i - 3)):
    print("*", end="")
  print("")

it's exactly the same but instead of abusing the print function it simply prints "*" j times in a loop.

yeah sorry about that.. the orig post was posted using my android phone xD so had to simplify my description..

Link to comment
Share on other sites

Link to post
Share on other sites

n=int(input("How many rows do you want?: "))

for i in range(n*2-1):
    for j in range(abs(n-i-1),n,1):
        print("*",end="")
    print("")

after so many nights understanding shit and energy drinks.. finally got the code to make it work XD

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×