Python Exercise11

Write a program to take input from command line and print all the values

from sys import argv

script,first, second =argv

print (“The script is called:”,script)

print( “The first variable is :”,first)

print (“Your Second variable is :”, second)

Write a program to get input from command line and add those values

import sys

print(len(sys.argv))

if (len(sys.argv)>3):

sum=0

for i in sys.argv[1:]:

sum=sum+int(i)

print(sum)

else:

print (“Please give atleast two parameters only”)

print( “My name=”,sys.argv[0])

print (“First arg”,sys.argv[1])

print( “Second arg”,sys.argv[2])

Write a program to get input from command line and print number of arguments received

import sys

print (“My name is {}”.format(sys.argv[1]))

print (“I received {} arguments”.format(len(sys.argv)-1))

Write a program to find the factorial by passing  the values from the command line

import math

import sys

for x in sys.argv[1:]:

print(“{} – {}”.format(x,math.factorial(int(x))))

Program to find the factorial by passing it from the command line, if the user does not pass the values , system should prompt for the input

import math

import sys

if len(sys.argv) == 1 :

num=input(“Enter a number: “)

print(math.factorial(int(num)))

else:

for x in sys.argv[1:]:

print(“{} – {}”.format(x,math.factorial(int(x))))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s