0

Hi so I got a problem with a nested loop where the output must be like this:

10
2030
304050
40506070
5060708090

but now I only know to do this which is:

10
1020
102030
10203040
1020304050

and this is my code currently:

def number(n):
    num = 0
    for i in range(0, n):
        num = 10

        for j in range(0, i + 1):
            print(num, end="")
            num = num + 10
        print("\r")
n = 5
number(n)

so how I change the code so that the output should be the same as the output needed?

3
  • 2
    Can you think of a rule that tells you what number to use at the start of a line? Can you think of a way to use that rule to change the numbers that you print? Commented Mar 25, 2020 at 1:26
  • 4
    It is probably better to work this out yourself because this question is not about programming knowledge or technical difficulties, but problem-solving skills, which the question is trying to help you develop. Commented Mar 25, 2020 at 1:31
  • 1
    @Pandafiqz, keep an eye on the variable num and see where it's being changed. Debug (or simply dry run) the code and see when the value is updated and if it's being updated correctly. Commented Mar 25, 2020 at 1:34

2 Answers 2

1

You can try this:

def contnum(n): 
    for i in range(0, n): 
        for j in range(0, i + 1): 
          print(i * 10 + 10, end="") 
          i = i + 1
        print("\r") 

n = 5
contnum(n) 

Output:

10
2030
304050
40506070
5060708090
Sign up to request clarification or add additional context in comments.

Comments

0

change the first num to 10 and get rid of the second num.

def number(): 
  num = 10 
  for i in range(0, num): 
    for j in range(0, i + 1): 
    print(num, end="") 
    num = num + 10 
  print(" ") 
number()

You should try to shorten this code a little:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.