我正在做一个Python类介绍的项目,我必须创建一个成绩册。在这本成绩簿中,我需要显示每个成绩字母值的学生数量并将其写入文件。一切正常,它不会破坏或任何东西,但在显示时,它看起来像这样:
(1, " students got A's")
(2, " students got B's")
(3, " students got C's")
(4, " students got D's")
(5, ' students failed')
当我希望它看起来像这样:
1 students got A's
2 students got B's
...and so on
我该如何解决这个问题?我可以把它原样转过来,但我宁愿输出看起来也不错。完整代码供参考:
#1 Open file
WriteTo=open("ClassNameGrades.txt", "w")
#2 Initialize variables
MoreStudents="Yes"
A=0
B=0
C=0
D=0
F=0
#3 Inputs and writing
ClassName=input("What is the class called? ") + '\n'
TeacherName=input("What is the teacher's name? ") + '\n'
WriteTo.write(ClassName)
WriteTo.write(TeacherName)
#4 Loop student inputs
while MoreStudents=='Yes':
Name=input("What is the student's name? ")
Grade=input("What is the student's grade number? ")
WriteName=str(Name)
SpaceName = ": "
IntGrade=int(Grade)
WriteGrade=Grade + '\n'
WriteTo.write(WriteName)
WriteTo.write(SpaceName)
WriteTo.write(WriteGrade)
#5 Assigning letter to grade
if IntGrade>=90:
A=A+1
elif IntGrade>=80:
B=B+1
elif IntGrade>=70:
C=C+1
elif IntGrade>=60:
D=D+1
else:
F=F+1
MoreStudents=input("Are there any more students? 'Yes' or 'No'")
#6 Assigning what to write in the file
PrintA=A," students got A's"
PrintB=B," students got B's"
PrintC=C," students got C's"
PrintD=D," students got D's"
PrintF=F," students failed"
#7 Writing to file
WriteTo.write(str(PrintA))
WriteTo.write("\n")
WriteTo.write(str(PrintB))
WriteTo.write("\n")
WriteTo.write(str(PrintC))
WriteTo.write("\n")
WriteTo.write(str(PrintD))
WriteTo.write("\n")
WriteTo.write(str(PrintF))
#8 Display in program
print(ClassName)
print(TeacherName)
print(PrintA)
print(PrintB)
print(PrintC)
print(PrintD)
print(PrintF)
答案 0 :(得分:2)
执行此操作时:
PrintA=A," students got A's"
...创建一个包含两个值的元组。
当您在这样的元组上调用str
时:
WriteTo.write(str(PrintA))
你得到的是括号中的两个值,正如你所看到的那样。
如果你想以某种特定的方式格式化结果,你必须明确地这样做,而不是制作一个元组:
PrintA = f"{A} students got A's"
# ...
WriteTo.write(PrintA)
...或者不只是在元组上调用str
:
WriteTo.write(f"{PrintA[0]} {PrintA[1]}")
对于您的情况,第一个版本可能更具可读性,但在某些现实应用中,可能有理由更喜欢第二个版本。
“f-string”格式化需要Python 3.6。如果您使用的是3.5或2.7,则可以使用str.format
代替:
PrintA="{} students got A's".format(A)
...如果您发现format
难以理解,您可以直接使用字符串方法:
PrintA = str(A) + " students got A's"
答案 1 :(得分:1)
假设你已经得到了这个结果:
(1, " students got A's")
(2, " students got B's")
(3, " students got C's")
(4, " students got D's")
(5, ' students failed')
对于其中的每一个,您只需将第一个元素更改为字符串并将它们连接在一起。
def helper(in):
return str(in[0]) + in[1]
helper((5, ' students failed'))
>>>"5 students failed"
只需将函数放在代码中并使用它修改要打印的每个内容:
WriteTo.write(helper(PrintA))
答案 2 :(得分:0)
如下的行:
PrintA=A," students got A's"
PrintB=B," students got B's"
PrintC=C," students got C's"
PrintD=D," students got D's"
PrintF=F," students failed"
实际上定义了tuples
,当打印出tuple
时,它首先转换为str
,这使它看起来像(x, y)
,因此它会打印出来:
(1, " students got A's")
相反,您要做的是直接创建str
,如下所示:
PrintA=str(A) + " students got A's"
PrintB=str(B) + " students got B's"
PrintC=str(C) + " students got C's"
PrintD=str(D) + " students got D's"
PrintF=str(F) + " students failed"
因此,当它打印出来时(作为str
),它会像:
1, students got A's