从 Python第 6 章练习开始:
Data in grades.txt
Jennifer 89
Pearson 90
Nancy 95
Gina 100
Harvey 98
Mike 99
Ross 15
Test 90
file=open('grades.txt','r')
my_list=[]
num_of_records=0
highest_score=1
highest_score_name=''
for line in file:
name,score=line.strip().split()
if int(score)>highest_score:
highest_score=int(score)
highest_score_name=name
num_of_records=num_of_records+1
print('the name and score of the record with highest score:')
print('Name:',highest_score_name)
print('Score:',highest_score)
print('\nNumber of records:',num_of_records)
file.close()
这里是 python 的入门者,并试图通读这本书,但是在这个问题上遇到了错误。
错误:
line 9, in <module> name,score=line.strip().split()
ValueError: not enough values to unpack (expected 2, got 0)
任何指南表示赞赏。
file = open('grades.txt', 'r')
num_of_records = 0
highest_score = 1
highest_score_name = ''
for line in file:
line = line.strip()
# Check whether the line is empty
if line == '':
continue
name, score = line.split()
if int(score) > highest_score:
highest_score = int(score)
highest_score_name = name
num_of_records = num_of_records+1
print('the name and score of the record with highest score:')
print('Name:', highest_score_name)
print('Score:', highest_score)
print('\nNumber of records:', num_of_records)
file.close()
希望对你有帮助:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句