我已为编码编写此代码,但它无法正常工作。我希望我的程序在输入3个测试的名称和等级时验证3个用户的用户输入。我的程序只检查第一个输入,然后询问其他用户名,并跳过询问用户输入或验证输入。
validInput1 = False
validInput2 = False
validInput3 = False
studentnames = []
studentMarkTest1 = []
studentMarkTest2 = []
studentMarkTest3 = []
totalScores = []
sum = 0
for i in range(3):
sname = input("Enter Student name:")
while not validInput1:
score1 = int(input("What did {} get on their test 1?".format(sname)))
if score1 < 0 or score1 >20:
print("Invalid input")
else:
validInput1 = True
while not validInput2:
score2 = int(input("What did {} get on their test 2?".format(sname)))
if score2 < 0 or score2 >25:
print("Invalid input")
else:
validInput2 = True
while not validInput3:
score3 = int(input("What did {} get on their test 3?".format(sname)))
if score3 < 0 or score3 >35:
print("Invalid input")
else:
validInput3 = True
totalScore = score1+ score2+ score3
sum = sum + totalScore
AverageTestScore = sum / 3
# saving name and grade
studentnames.append(sname)
studentMarkTest1.append(score1)
studentMarkTest2.append(score2)
studentMarkTest3.append(score3)
totalScores.append(totalScore) for i in range(3):
print(studentnames[i],"total Test score",totalScores[i]) print("class average", AverageTestScore)
这里运行程序时会发生什么
>>>
Enter Student name:g
What did g get on their test 1?44
Invalid input
What did g get on their test 1?33
Invalid input
What did g get on their test 1?44
Invalid input
What did g get on their test 1?22
Invalid input
What did g get on their test 1?20
What did g get on their test 2?34
Invalid input
What did g get on their test 2?23
What did g get on their test 3?55
Invalid input
What did g get on their test 3?44
Invalid input
What did g get on their test 3?32
Enter Student name:e
Enter Student name:e
g total Test score 75
e total Test score 75
e total Test score 75
class average 75.0
>>>
如何获得存储在totalscore中的最高测试分数值,然后使用得分最高的学生的姓名打印出最高分?
答案 0 :(得分:0)
您需要在validInput
循环之后将False
放回while
,或者更好地将其替换为for
循环的开头。
答案 1 :(得分:0)
让我们来看看你的while循环:
while not validInput1:
第一次运行应用程序时,validInput1
为False
,因此此块会进行评估。但是,在后续循环(来自for i in range(3):
)上,validInput1
已经为真,因此完全跳过此块。
您可以通过在for循环开头将validInput1
,validInput2
和validInput3
更改为False
来解决此问题。
答案 2 :(得分:0)
这是因为validInput1,validInput2和validInput3变量在第一次迭代时设置为True,但在第二次迭代时未设置为false。
因此,对于第一次迭代后,由于&#34; not True&#34;即假
可以在每次迭代后再次将它们设置为false,即在循环结束时
答案 3 :(得分:0)
从一个函数开始,只获取一个学生的详细信息
def get_student_detail(num_tests): # get the detail for just 1 student!
student_name = input("Enter Student Name:")
scores = []
for i in range(1,num_tests+1):
scores.append(float(input("Enter Score on test %s:"%i)))
return {'name':student_name,'scores':scores,'avg':sum(scores)/float(num_tests)}
现在只为每个学生打电话
student_1 = get_student_detail(num_tests=3)
student_2 = get_student_detail(num_tests=3)
student_3 = get_student_detail(num_tests=3)
print("Student 1:",student_1)
print("Student 2:",student_2)
print("Student 3:",student_3)
或者更好地实现一个让你可以随心所欲的功能
def get_students():
resp="a"
students = []
while resp[0].lower() != "n":
student = get_student_detail(num_tests=3)
students.append(student)
resp = input("Enter another students details?")
return student
您可能希望拆分get_student_details
甚至进一步编写方法以获得单个测试分数并验证其实际是一个数字(如果您输入的数字不是一个数字作为测试分数,这将会崩溃),或者在给出空响应之前继续询问测试分数等等
def get_number(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print ("That is not a valid number!")
def get_test_score(prompt):
while True:
score = get_number(prompt)
if 0 <= score <= 100:
return score
print("Please enter a value between 1 and 100!")
test_score = get_test_score("Enter test score for test 1:")
print(test_score)