Jump to content
15 minutes ago, Sauron said:

What's that supposed to do?

*

**

***

****

***

**

*

 

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

Link to comment
https://linustechtips.com/topic/1105481-python-nested-loops/#findComment-12900731
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
https://linustechtips.com/topic/1105481-python-nested-loops/#findComment-12900805
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
https://linustechtips.com/topic/1105481-python-nested-loops/#findComment-12900846
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
https://linustechtips.com/topic/1105481-python-nested-loops/#findComment-12900955
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

×