Recording the history of Nellore

Uncategorized

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

Discover more from Nellorean

Subscribe now to keep reading and get access to the full archive.

Continue reading