Home > front end >  I'm learning and tyring to create a for loop
I'm learning and tyring to create a for loop

Time:05-09

I am trying to create a for loop and I want the output to be this:

0 is even.
1 is odd.
2 is even.
3 is odd.
4 is even.
5 is odd.
6 is even.
7 is odd.
8 is even.
9 is odd.

And all I have gotten this far:

my_range=range(0,11)

for i in my_range:
    print(i)

and the output is all the numbers:

0
1
2
3
4
5
6
7
8
9
10

CodePudding user response:

You just need to do is, change your print function as -

print(i," is", 'even.' if (i % 2 == 0) else 'odd.')

CodePudding user response:

What you need to use here is conditional statements along with 'String Interpolation' in python.

The Below code will give you the desired output:

my_range=range(0,11)

for i in my_range:
    if i%2:
        print("{} is odd".format(i))
    else:
        print("{} is even".format(i))
  • Related