我的代码仍未完成。我试图从文件中获取基于数字等级的字母等级,然后将其写入新文件。我仍然认为我不能正确理解函数以及如何在main中调用它们。
我想我知道numeric_grade不能读取为整数。但是我不确定我的代码在哪里使用“ int”:
# Get letter grade for students, undergrad and grad, and catch invalid number grades an invalid categories.
def get_letter_grade(numeric_grade, student_type):
# Catch errors for invalid numbers for grades.
while (numeric_grade < 0) or (numeric_grade > 100):
# Display error message for invalid numbers.
print('Error occurred while determining letter grade. Aborting. ')
# Student number grade if GRAD student:
if numeric_grade >= 95 and student_type == 'GRAD':
letter_grade = 'H'
return letter_grade
elif (numeric_grade >= 80) and (numeric_grade < 95) and student_type == 'GRAD':
letter_grade = 'P'
return letter_grade
elif (numeric_grade >= 70) and (numeric_grade < 80) and student_type == 'GRAD':
letter_grade = 'L'
return letter_grade
elif (numeric_grade >= 0) and (numeric_grade < 70) and student_type == 'GRAD':
letter_grade = 'F'
return letter_grade
# Student number grade if UNDERGRAD student:
elif numeric_grade >= 90 and student_type == 'UNDERGRAD':
letter_grade = 'A'
return letter_grade
elif (numeric_grade >= 80) and (numeric_grade < 90) and student_type == 'UNDERGRAD':
letter_grade = 'B'
return letter_grade
elif (numeric_grade >= 70) and (numeric_grade < 80) and student_type == 'UNDERGRAD':
letter_grade = 'C'
return letter_grade
elif (numeric_grade >= 60) and (numeric_grade < 70) and student_type == 'UNDERGRAD':
letter_grade = 'D'
return letter_grade
elif (numeric_grade >= 0) and (numeric_grade < 60) and student_type == 'UNDERGRAD':
letter_grade = 'F'
return letter_grade
# Run main program.
def main():
# Prompt the user to enter the name of the input file.
in_filename = input('Please enter the name of the input data file: ')
# Open input file in read mode.
try:
in_file = open(in_filename, 'r')
# Make sure that the file exists and ask the user to re-enter a filename if needed.
except FileNotFoundError:
print(in_filename, ' could not be opened. Please restart.')
# Prompt the user to enter the name of an output file.
out_filename = input('Please enter the name of the output data file: ')
# The output file should be erased/overwritten if an old one with the same name exists.
out_file = open(out_filename, 'w')
# Prime the loop by reading first record (first 3 lines).
category = in_file.readline()
student_name = in_file.readline()
num_grade = in_file.readline()
# Write first record to output file.
out_file.write(category)
out_file.write(student_name)
out_file.write(num_grade)
# Read input file one line at a time:
while category != '' and student_name != '' and num_grade != '':
# Read input line for student category.
category = in_file.readline()
# Read input line for student name.
student_name = in_file.readline()
# Read input line for grade.
num_grade = in_file.readline()
# Remove the newlines.
category = category.rstrip('\n')
student_name = student_name.rstrip('\n')
num_grade = num_grade.rstrip('\n')
# Write output file.
out_file.write(str(category + '\n'))
out_file.write(str(student_name + '\n'))
out_file.write(str(num_grade + '\n'))
# Assign grades as appropriate for type of student (GRAD vs UNDERGRAD).
letter_grade = get_letter_grade(num_grade, category)
while letter_grade != '':
out_file.write(letter_grade)
letter_grade = get_letter_grade(num_grade, category)
in_file.close()
out_file.close()
main()
答案 0 :(得分:1)
如果您不确定数据类型的可靠性,则在调用如下函数时,我会将输入转换为浮点数;
letter_grade = get_letter_grade(num_grade=float(num_grade), category=category)
这将把num_grade
转换为数字(如果是字符串)。如果已经是一个数字,那么这样做仍然没有害处。
此外,如果您希望通过另一种方式在命令行中读取文件,则我建议使用sys
,因为这将使您一次读取所有内容(因此您可以向上翻页并轻松地重复该功能无需重新键入),也可以使用制表符完整来确保文件存在。这是一个例子;
import sys
def main(input_file, output_file):
... main code goes here
if __name__ == "__main__":
main(*sys.argv[1:])
在这里sys.argv将您键入的内容(在作为函数的第一个参数之后)输入命令行,并将其作为参数插入到函数中。
因此您可以通过键入来使用它
python file_name.py <input_file_path/in_filename > <output_filepath/in_filename >
进入您的终端
,它将执行main(input_file=input_file_path, output_file=output_filepath)
希望有帮助!
答案 1 :(得分:0)
您需要在这里了解两件事:
因此模式通常是:
总结起来,理想的放置变量的地方是在这一行之后:
num_grade = num_grade.rstrip('\n') ,
但在将其传递给函数get_letter_grade之前。