この課題では、Python でクラスを作成して、3 つのスコアを配置し、学生の平均と成績を見つける必要があります。動作しますが、エラーが発生しています。整数 (例: 73) を 3 回入力すると、文字の等級が表示されます。しかし、小数を含む数字 (83.7 など) を 3 回入力すると、文字の等級が表示されません。これが必要な種類の数字です。小数で数字を入力したときに成績を表示させる方法はありますか? そして、なぜそれは整数に対してのみ機能するのですか?
class grades:
def __init__(self,name,test1,test2,test3,avg):
self.name = name
self.test1 = test1
self.test2 = test2
self.test3 = test3
self.avg = avg
def getName(self):
return self.name
def getTest1(self):
return self.test1
def getTest2(self):
return self.test2
def getTest3(self):
return self.test3
def getAvg(self):
return self.avg
#main:
name = input("Enter the students name: ")
test1 = float(input("Enter the first score: "))
test2 = float(input("Enter the second score: "))
test3 = float(input("Enter the third score: "))
avg = float(test1 + test2 + test3) /3.0
grades1 = grades(name,test1,test2,test3,avg)
grades1.name
grades1.test1
grades1.test2
grades1.test3
grades1.avg
print("The student's name is:" ,grades1.name)
print(grades1.name +"'s test scores are:")
print("---------------------------:")
print("TEST1: \t\t\t",grades1.test1)
print("TEST2: \t\t\t",grades1.test2)
print("TEST3: \t\t\t",grades1.test3)
print(grades1.name +"'s average is: \t",grades1.avg)
##if avg <= 100.0 and avg >= 90.0:
## print(name +"'s grade is: \t A")
##elif avg <= 89.0 and avg >= 80.0:
## print(name +"'s grade is: \t B")
##elif avg <= 79.0 and avg >= 70.0:
## print(name +"'s grade is: \t C")
##elif avg <= 69.0 and avg >= 60.0:
## print(name +"'s grade is: \t D")
##elif avg <= 59.0 and avg >= 0.0:
## print(name +"'s grade is: \t E")
if avg >= 90.0 and avg <= 100.0:
print(name +"'s grade is: \t A")
elif avg >= 80.0 and avg <= 89.0:
print(name +"'s grade is: \t B")
elif avg >= 70.0 and avg <= 79.0:
print(name +"'s grade is: \t C")
elif avg >= 60.0 and avg <= 69.0:
print(name +"'s grade is: \t D")
elif avg >= 0.0 and avg <= 59.0:
print(name +"'s grade is: \t E")
1年生の部分をコメントした理由は、両方の方法を試してみたがうまくいかなかったからです.
これは私が試したものですが、小数では運がありません
Enter the students name: k
Enter the first score: 89.9
Enter the second score: 89.9
Enter the third score: 89.9
The student's name is: k
k's test scores are:
---------------------------:
TEST1: 89.9
TEST2: 89.9
TEST3: 89.9
k's average is: 89.90000000000002