using python 3 1Suppose a program that uses command line arg
using python 3.
1)Suppose a program that uses command line arguments starts with the following line: import sys . Write the Python statement that will print the number of arguments that were provided on the command line.
2}Suppose the user intends to run the program by supplying command-line arguments , where an operation is supplied (capitalize, lowercase, count) followed by any number of strings. capitalize: changes all letters in the word to uppercase, lowercase: changes all letters in the word to lowercase,count: returns a count of all of the letters in the strings. Write a program that processes the command line arguments correctly.
 Examples:
python myProg.py capitalize This is easy
 output would be THIS IS EASY
 or
 python myProg.py lowercase This WAS easy as Pie
 output would be this was easy as pie
 or
 python myProg.py count This WAS, easy! as Pie.
 output would be 16
 or
 python myProg.py lower This WAS easy as Pie
 output would be “ Invalid operation”
3)Write a Python program that accepts the number of scores followed by that many scores via the command line and prints the average of the scores. Include appropriate error-checking to make sure command line arguments are provided. No other error checking is required.
 Example1:   python   calcAvg.py   5 100 90 70 80 90
 Output of the code should be:   Average: 87
 Example2: python calcAvg.py 3 100 90 80
 Output of the code should be:   Average: 90
 Example 3: python calcAvg.py
 Output of the code should be:    No scores were provided – average cannot be calculated.
4)Suppose the user is expected to supply words on the command line as shown below:
 Example: python myProg.py hello there
 Output of the code should be:   
 Average word length: 5 Number of Vowels: 4
 Write Python code to correctly produce the output shown.
| using python 3. 1)Suppose a program that uses command line arguments starts with the following line: import sys . Write the Python statement that will print the number of arguments that were provided on the command line. | |
| 2}Suppose the user intends to run the program by supplying  command-line arguments , where an operation is supplied  (capitalize, lowercase, count) followed by any number of strings.  capitalize: changes all letters in the word to uppercase,  lowercase: changes all letters in the word to lowercase,count:  returns a count of all of the letters in the strings. Write a  program that processes the command line arguments correctly. python myProg.py capitalize This is easy | |
| 3)Write a Python program that accepts the number of scores  followed by that many scores via the command line and prints the  average of the scores. Include appropriate error-checking to make  sure command line arguments are provided. No other error checking  is required. | |
| 4)Suppose the user is expected to supply words on the command  line as shown below: | 
Solution
1)
Code:
import os
 import sys
 if __name__ == \"__main__\":
 print len(sys.argv)-1
2)
code:
import os
 import sys
 if __name__ == \"__main__\":
 choice = str(sys.argv[1])
 st=\"\"
 for i in range(2,len(sys.argv)):
 st+=sys.argv[i]
 if choice == \"capitalize\":
 print st.upper()
 elif choice == \"lowercase\":
 print st.lower()
 else:
 print len(st)   
   
3) code:
import os
 import sys
 if __name__ == \"__main__\":
 summ=0
 count=0
 for i in range(1,len(sys.argv)):
 count+=1
 summ+=int(sys.argv[i])
 print float(summ)/float(count)
4) code:
import os
 import sys
 if __name__ == \"__main__\":
 st=\"\"
 for i in range(1,len(sys.argv)):
 st+= sys.argv[i]+\" \"
 print st
 vowel = \"aeiou\"
 novow = 0
 for c in st:
 if c in vowel:
 novow+=1
 li = st.split()
 summ=0
 for l in li:
 summ+=len(l)
 print \'no of vowels: \'+str(novow)
 print \'average word length: \'+str(float(summ)/float(len(li)))



