I need help modifying this python program without using with or any lists, this
ID: 3706332 • Letter: I
Question
I need help modifying this python program without using with or any lists, this includes split.
import sys
for i in range(1, len(sys.argv)):
grade = 0
count = 0
for char in sys.argv[i]:
if char == ',':
if count == 0:
count += 1
print(': ', end='')
else:
print(' ', end='')
else:
print(char, end='')
print()
this program reformats sys.argv input: Mary,IT3233,B John,IT2430,A
Joe,IT3233,A Bob,IT2430,B.
i need it to ALSO:
Extend it such that if the grade is invalid, skip it. Valid grades: A, B, C, D, F.
Extend further such that you count the number of distinct courses and distinct grades for valid grades.
Output for above would be IT 3233 – 2, IT 2430 – 2, A-2, B-2.
Explanation / Answer
Please find the below program with necessary changes :
import sys
#list is declared to store all the command-line argument value and compare them
myList = []
#character type variable is deaclared to store each character from command-line
a = ''
for i in range(1, len(sys.argv)):
grade = 0
count = 0
for char in sys.argv[i]:
if char == ',':
#adding each word to list
myList.append(a)
print(a)
a = ''
if count == 0:
count += 1
print(': ', 'end=' , count)
else:
print(' ', 'end=' , count)
else:
#adding each character to make a complete word seperated by ','
a=a+char
print(a)
#appending the grade to the myList
myList.append(a)
#removing the names from myList
y = len(myList)
myList.pop(y - 3)
print(myList)
#removing the entry with invalid grade
if a!='A' and a!='B' and a!='C' and a!='D' and a!='F':
x = len(myList)
print(x)
myList.pop(x - 1)
myList.pop(x - 2)
myList.pop(x - 3)
print(myList)
#counting number of distinct courses and distinct grades for valid grades
for i in range(len(myList)):
count = 1
for j in range(i+1, len(myList)):
if myList[i] == myList[j]:
count += 1
if count > 1:
print myList[i],count
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.