Skip to content Skip to sidebar Skip to footer

Unable To Get Wanted Output Using For And Range Functions

For my homework assignment, I am using the for loop and the range function. I have to create a loop that prints Hello 0 Hello 1 Hello 3 Hello 6 Hello 10 The question says that the

Solution 1:

may be this will be suitable :

i=0step=1while i<=10:
    print("Hello", i)
    i=i+stepstep=step+1

Solution 2:

the integers can be formed by cumulatively adding from 0 to 5 like so:

x = 0for n in range(1,6):
    print(x)
    x += n

Output:

0
1
3
6
10

If you must add, a string Hello to each, depending on how far you are in your learning, there are 3 ways to go about this. Either:

  1. text = 'Hello %s' % x - SUPER OLD string formatting for Python.
  2. text = 'Hello {}'.format(x) - called %-formatting.
  3. text = f'Hello {x}' - called F-strings suggested.

So to complete the code:

x = 0for n inrange(1,6):
    print(f'Hello {x}')
    x += n

Output:

Hello 0
Hello 1
Hello 3
Hello 6
Hello 10

Post a Comment for "Unable To Get Wanted Output Using For And Range Functions"