Exercise 1: CommandLine,Input,Print

Example 1: Provide two numbers as input from commandline args and show their average.

import sys
args = sys.argv
if not len(args)>2:#As filename+2numbers = total 3 arguments.
    print("Please provide valid input data")
else:
    sum = float(args[1])+float(args[2])
    average = sum/2
    print("Average of ",args[1]," and ",args[2]," = ",average)

Example 2: Above question,change from command line to keyboard input..i.e. by input() function.

num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))

average = (num1+num2)/2
print("Average of ",num1," and ",num2," = ",average)

Example 3:Write a program to read values to a,b,c and display x,where x=a/b-c

a,b,c=34,45,67
x=a/b-c
print(x)

 

Leave a comment