转换需要.txt文件的程序并修改为函数 [英] Converting a program that takes in a .txt file and modifying to functions
问题描述
完全卡在函数上....
Completely stuck on functions....
我有一个.txt文件:
I have a .txt file:
Bronson 90 85 75 76
Conor 90 90 90 90
Austyn 50 55 32 75
Mark 96 95 85 85
Anthony 85 85 85 85
我正在尝试采用此代码(如下).并将其转换为函数以产生相同的输出:
And I'm trying to take this code (below). And turn it into functions to produce the same output:
with open(argv[1]) as f:
names = []
for line in f:
names.append(line.split()[0])
print('\nNames of students who have written tests:')
print(*sorted(names), sep=' ')
# prompt user to input which student you want to view
name = input('Enter name of student whose test results '
'you wish to see: ')
if name not in names:
print('\nNo test data found for ', name)
input("Press Enter to continue ...")
else:
name_print = "\nSummary of Test Results for " + name
# print test scores of the student selected
with open(argv[1]) as f:
for line in f:
parts = line.split()
if parts[0] == name:
print(name_print)
print('=' * ((len(name_print)) - 1))
test_scores = ' '.join(parts[1:])
print('Test scores: ', end=' ')
print(test_scores)
当前输出:
Names of students who have written tests:
Anthony Austyn Bronson Conor Mark
Enter name of student whose test results you wish to see: Anthony
Summary of Test Results for Anthony
===================================
Test scores: 85 85 85 85
Number of tests written .................. 4
我想使用 process_marks(f)
使其在一个main()模块上运行:
I want to make it run on one main() module using process_marks(f)
:
from functions import process_marks
def main():
try:
f = open(argv[1])
except FileNotFoundError:
print("\nFile ", argv[1], "is not available")
exit()
process_marks(f). #The program essentially runs in this function with help from helpers
f.close()
main()
这是我目前拥有的:
def process_marks(f):
names = []
for line in f:
names.append(line.split()[0])
print('\nNames of students who have written tests:')
names = sorted(names)
print(*names, sep=' ')
name = input('Enter name of student whose test results '
'you wish to see: ')
check_name(name, names)
print_grades(name, line)
def check_name(name, names):
if name not in names:
print('\nNo test data found for ', name)
input("Press Enter to continue ...")
else:
name_print = "\nSummary of Test Results for " + name
print(name_print)
print('=' * ((len(name_print)) - 1))
def print_grades(name, line):
test_scores = ' '.join(line[1:])
print('Test scores: ', end=' ')
print(test_scores)
这是使用上面的代码输出的内容:
This is what outputs using above code:
Names of students who have written tests:
Anthony Austyn Bronson Conor Mark
Enter name of student whose test results you wish to see: Mark
Summary of Test Results for Mark
================================
Test scores: n t h o n y 8 5 8 5 8 5 8 5
我不想远离当前的代码(我想使用列表等,因为id最终希望包括平均值,最大值,最小值等.但是,我已经对此进行了一段时间的研究在获取代码以访问与名称对应的行时遇到明显的问题.
I don't want to go to far away from the current code I have (i want to use lists etc because id like to eventually include averages, max, min etc. However, I have been grinding over this for a while having obvious problems getting the code to access the line that corresponds to the name.
推荐答案
您的方法崩溃了一些.
例如以下行:
print_grades(name, line)
将使用上述for循环中 line
的最后一个值.永远是"Anthony"
Will use the last value of line
from the for loop above. Which will always be "Anthony"
您可以尝试按以下方法将值从方法传递到方法:
You can try to pass the values from method to method as below:
from sys import argv
def get_names(my_file):
names = []
for line in my_file:
names.append(line.split()[0])
print('\nNames of students who have written tests:')
print(*sorted(names), sep=' ')
return names
def get_and_check_name(names):
name = input('Enter name of student whose test results '
'you wish to see: ')
if name not in names:
print('\nNo test data found for ', name)
input("Press Enter to continue ...")
else:
name_print = "\nSummary of Test Results for " + name
return name, name_print
def print_scores(f, name, name_print):
for line in f:
parts = line.split()
if parts[0] == name:
print(name_print)
print('=' * ((len(name_print)) - 1))
test_scores = ' '.join(parts[1:])
print('Test scores: ', end=' ')
print(test_scores)
def process_marks(f):
While True:
names = get_names(f)
name, name_print = get_and_check_name(names)
f.seek(0)
print_scores(f, name, name_print)
yes_no = input('\nDo again for another student?')
if yes_no == 'n':
break
continue
with open(argv[1]) as f:
process_marks(f)
这篇关于转换需要.txt文件的程序并修改为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!