jeudi 22 décembre 2016

Python if and else print condition

If an integer is divisible by 3, print "Hi"

If it is divisible by 7, print "Bye"

If it is divisible by both 3 and 7, print "HiBye"

As of now I have tried:

for i in range(1,100):
    if i % 3 == 0:
        print "Hi"
    if i % 7 == 0:
        print "Bye"
    if i % 3 == 0 and i % 7 == 0:
        print "HiBye"
    else: 
        print i

But my numbers are repeated. i.e. this is the output I get.

1
2
Hi
3
4
5
Hi
6
Bye
7
8
Hi
9
10
11
Hi
12
13
Bye
14
Hi
15
16
17
Hi
18
19
20
Hi
Bye
HiBye

As you can see, the 3 is repeated again. I think the mistake is in the

else:
    print i

statement

Aucun commentaire:

Enregistrer un commentaire