Reputation: 99
My aim : Write a Menu driven python program that user to pass the value in command line, a. Count number of characters, special characters and Numbers. b. Add all the numbers and display the sum is odd or even. c. Sort the characters in descending order. My code for this:
from sys import argv
print(argv[1:])
print("enter 1 for counting, 2 for sum and 3 for sorting the letter")
choice=int(input("enter choice: "))
if choice==1:
alphabets = digits = special = 0
for i in range(1,len(argv)):
if((argv[i] >= 'a' and argv[i] <= 'z') or (argv[i] >= 'A' and argv[i] <= 'Z')):
alphabets = alphabets + 1
elif(argv[i] >= '0' and argv[i] <= '9'):
digits = digits + 1
else:
special = special + 1
print("Total Number of Alphabets : ", alphabets)
print("Total Number of Digits : ", digits)
print("Total Number of Special Characters : ", special)
elif choice==2:
sum=0
for i in range(1,len(argv)):
if(argv[i].isdigit()):
sum=sum+int(argv[i])
print(sum)
if sum%2==0:
print("sum is even")
else:
print("sum is odd")
elif choice==3:
s=""
for i in range(1,len(argv)):
if((argv[i] >= 'a' and argv[i] <= 'z') or
(argv[i] >= 'A' and argv[i] <= 'Z')):
s=s+argv[i]
# print(s)
def sortdes(str):
str.sort(reverse = True)
str1 = ''.join(str)
print(str1)
s1=list(s)
sortdes(s1)
But I don't get the desired output. For example, if my input is 543(in command line), My output is:
['543']
enter 1 for counting, 2 for sum and 3 for sorting the letter
enter choice: 1
Total Number of Alphabets : 0
Total Number of Digits : 1
Total Number of Special Characters : 0
>>>
['543']
enter 1 for counting, 2 for sum and 3 for sorting the letter
enter choice: 2
543
sum is odd
>>>
['543']
enter 1 for counting, 2 for sum and 3 for sorting the letter
enter choice: 3
>>>
How do I rectify this? Please do help.Thanks in advance for your time and help:)
Upvotes: 0
Views: 720
Reputation: 173
i am assuming that you are passing only one argument, you need to replace argv[i] with argv[1][i] wherever applicable
from sys import argv
print(argv[1])
print("enter 1 for counting, 2 for sum and 3 for sorting the letter")
choice=int(input("enter choice: "))
if choice==1:
alphabets = digits = special = 0
for i in range(len(argv[1])):
if((argv[1][i] >= 'a' and argv[1][i] <= 'z') or (argv[1][i] >= 'A' and argvargv[1][i] <= 'Z')):
alphabets = alphabets + 1
elif(argv[1][i] >= '0' and argv[1][i] <= '9'):
digits = digits + 1
else:
special = special + 1
print("Total Number of Alphabets : ", alphabets)
print("Total Number of Digits : ", digits)
print("Total Number of Special Characters : ", special)
elif choice==2:
sum=0
for i in range(len(argv[1])):
if(argv[1][i].isdigit()):
sum=sum+int(argv[1][i])
print(sum)
if sum%2==0:
print("sum is even")
else:
print("sum is odd")
elif choice==3:
s=""
for i in range(len(argv[1])):
if((argv[1][i] >= 'a' and argv[1][i] <= 'z') or
(argv[1][i] >= 'A' and argv[1][i] <= 'Z')):
s=s+argv[1][i]
# print(s)
def sortdes(str):
str.sort(reverse = True)
str1 = ''.join(str)
print(str1)
s1=list(s)
sortdes(s1)
Upvotes: 1