dimanche 1 juillet 2018

multiple conditions in if statement using 'and'

Problem statement : IP addresses of the following forms are considered special local addresses: 10. * . * . * and 192.168. * . * . The stars can stand for any values from 0 to 255. Write a program that asks the user to enter an IP address and prints out whether it is in one of these two forms

My code :

s=input('Enter the IP address :')
if s[0]==1 and s[1]==0 and s[2]=='.':
    print('It is a special IP address')
elif s[0]==1 and s[1]==9 and s[2]==2 and s[3]=='.' and s[4]==1 and s[5]==6 
and s[6]==8:
    print('It is a special IP address')
else:
    print('It is an ordinary IP address')    

startwith() is a good option to solve this problem. However, i am unable to figure out why the above code always gives the output as 'It is an ordinary IP address' regardless of what the input is.

Any help is appreciated!

  • Line(5) => and s[6]==8 is just and extension of elif statement.

Aucun commentaire:

Enregistrer un commentaire