9) c. Command Line Arguments Examples




Wasabi is a 9 year old kid who is going house to house on Halloween to collect candies. After a tiring day of collection he wants to calculate the total number of candies that he has got. Write a program for Wasabi to help him do the same.

1
2
3
4
5
6
7
8
9
10
Program:
# add.py

import sys
sum = 0

for val in sys.argv[1:]:
    sum = sum + int(val)

print('The total number of candies are {}'.format(int(sum)))
1
2
3
Output:
C:\Examples> python add.py 23 53 62
The total number of candies are 138




Kraken is going for shopping and lets his wife write the items to buy on command line separated by space. Kraken is very particular in the output and wants the list to be in sorted order with the total number of items on top. Create the program for Kraken which does the same.

1
2
3
4
5
6
Program:
#shopping.py

import sys

print('The total number of items are {} and the list is \n{}'.format(len(sys.argv[1:]), '\n'.join(sorted(sys.argv[1:]))))
1
2
3
4
5
6
7
Output:
C:\Examples> python shopping.py "candy" "chocolates" "chips" "burgers"
The total number of items are 4 and the list is
burgers
candy
chips
chocolates




Jian is a budding architect. He is currently in middle school and recently learned about area of a rectangle. He wants to test his knowledge and wants to write a program that takes the length of 2 sides on command line and print the area of the same. Write a program that help Jian do the same.

1
2
3
4
5
6
7
Program:
#area.py
import sys

side1 = int(sys.argv[1])
side2 = int(sys.argv[2])
print('The area of rectangle is {}'.format(side1 * side2))
1
2
3
Output:
C:\Examples> python area.py 23 53
The area of rectangle is 1219




Xia has given his final semester exams and wants to find the average of his marks. He wants the program to be written in such a way that the input is taken from command line and the result is printed onto the screen. Help him write the program. For simplicity lets assume the marks would only be an integer value.

1
2
3
4
5
6
7
8
9
10
11
12
Program:
#average.py

import sys

total = 0
count = 0
for val in sys.argv[1:]:
    total = total + int(val)
    count = count + 1
avg = total/count
print('the average of marks is {}'.format(avg))
1
2
3
Output:
C:\Examples> python average.py 70 84 87 92 79 88
the average of marks is 83.33333333333333




Boris is an angry teen who is very infamous for his antics in his city. His parents has reached out to his principal, Xhala, for some help. Xhala has an idea to make Boris write Greetings for any person who has given his name to Boris. Boris instead has asked you to do the same for him. As you are afraid of Boris you want this to be created quickly. Write a program that greets the user of a program. The name of the user should be inputted from the command line and the program should add Greetings in front of the name.

1
2
3
4
5
6
Program:
#greetings.py

import sys

print('Greetings {}'.format(sys.argv[1]))
1
2
3
Output:
C:\Examples> python greetings.py "Roger 'Bawdy Boatswain' Moon - The Deadly Fist of Castaway Estuary!"
Greetings Roger 'Bawdy Boatswain' Moon - The Deadly Fist of Castaway Estuary!