In Python I am using this file Criteria.txt and it looks like this Homework,5,10
ID: 3663484 • Letter: I
Question
In Python I am using this file Criteria.txt and it looks like this
Homework,5,10,10
Quiz,3,20,20
Midterm,1,50,30
Final,1,100,40
I typed in this code to open and manipulate the Criteria.txt file
>>> criteria=open('Criteria.txt','r')
>>> criteria_line=criteria.readline()
>>> criteria_myline=criteria_line.strip().split(',')
>>> for criteria_line in criteria:
... print (criteria_line),
... criteria_myline = criteria_line.strip().split(',')
... print ("Assignment Type: "+criteria_myline[0])
... print ("Number: "+ criteria_myline [1])
... print ("Points: "+ criteria_myline [2])
... print ("Weight: "+ criteria_myline [3])
... print ()
and the result was this
Quiz,3,20,20
(None,)
Assignment Type: Quiz
Number: 3
Points: 20
Weight: 20
Midterm,1,50,30
(None,)
Assignment Type: Midterm
Number: 1
Points: 50
Weight: 30
Final,1,100,40
(None,)
Assignment Type: Final
Number: 1
Points: 100
Weight: 40
This was all correct except Homework didn't show up. You can see in the Criteria.txt file hat Homework was also in the list. What is wrong with this code? Why did it leave Homework out? What is the right code?
Explanation / Answer
I have added one line to your code which will print the line as per your expectations.
The following is the modified code.
>>> criteria=open('Criteria.txt','r')
>>> criteria_line=criteria.readline()
>>> criteria_myline=criteria_line.strip().split(',')
... print (criteria_line),
>> for criteria_line in criteria:
... print (criteria_line),
... criteria_myline = criteria_line.strip().split(',')
... print ("Assignment Type: "+criteria_myline[0])
... print ("Number: "+ criteria_myline [1])
... print ("Points: "+ criteria_myline [2])
... print ("Weight: "+ criteria_myline [3])
... print ()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.