IF and elif and else
#!/usr/bin/python
import sys
# By default argument will count script name also so I have reduced -1 for exact value
arg_count = len(sys.argv) - 1
#print what I have entered in arguments"
print "\nYou have entered ",arg_count, "arguments", sys.argv[1],",", sys.argv[2]
# Store Argurments in variable
Arg_first = sys.argv[1]
print "\nType of first argument is ", type(Arg_first)
# store second arg in variable
Arg_Sec = sys.argv[2]
print "\nType of second argument is", type(Arg_Sec)
#By default argement will be in string type so converting in integer for mathematical
a = int(Arg_first)
print "\nAfter converting argument type is", type(a)
b = int(Arg_Sec)
print "After converting second argument type is", type(b)
if a == b:
print "\nAddition of Argument 1 and Argument 2 is", a + b
elif a < b:
print "\nMultiple of Arg1 and Arg2 is", a * b
elif a > b:
print "\nDivide the arg1 and agr2 is", a / b
else:
print "I am wrong"
Output:
[root@mysql Scripts]# python if.py 4 2
You have entered 2 arguments 4 , 2
Type of first argument is
Type of second argument is
After converting argument type is
After converting second argument type is
Divide the arg1 and agr2 is 2
[root@mysql Scripts]# python if.py 4 8
You have entered 2 arguments 4 , 8
Type of first argument is
Type of second argument is
After converting argument type is
After converting second argument type is
Multiple of Arg1 and Arg2 is 32
[root@mysql Scripts]# python if.py 9 9
You have entered 2 arguments 9 , 9
Type of first argument is
Type of second argument is
After converting argument type is
After converting second argument type is
Addition of Argument 1 and Argument 2 is 18
Comments
Post a Comment