正如问题所说。我正努力从学生那里获得4个“实用”成绩,同时保持每个年级的界限在0-40之内。但是我继续使用以下代码收到错误:
while True:
try:
mark = int(input("What was your first mark out of the four? "))
if mark < 0 or mark > 40 :
print("That is outside the score boundaries. Please try again.")
else:
break
except ValueError:
print("That isn't a number!")
counter = 1
while counter <4:
while True:
try:
mark += int(input("And the next mark? "))
if mark < 0-mark or mark > 40+mark :
print("That is outside the score boundaries. Please enter the final three marks again.")
else:
break
except ValueError:
print("That isn't a number! Please enter the final three marks again.")
counter += 1
while True:
try:
end_mark = int(input("What was your end of year mark? "))
if end_mark < 0 or end_mark > 60 :
print("That is outside the score boundaries. Please try again.")
else:
break
except ValueError:
print("That isn't a number!")
#calculations
average = mark/counter
total_score = average + end_mark
#processing
if total_score > 50:
if end_mark < 30:
print("You pass conditionally with %{}. Your practical mark was averaged at {}, but your end of year mark was only {}.".format(total_score, average, end_mark))
else:
print("You pass with %{}. Your practical mark was averaged at {}, and your end of year mark was {}.".format(total_score, average, end_mark))
else:
print("Im sorry. You failed the year with %{}. Your practical mark averaged at {}, and the end of year mark was {}.".format(total_score, average, end_mark))
print("End of Code")
我怎么能解决这个问题?
答案 0 :(得分:0)
这是因为您首先添加然后检查,因此mark
永远不会是> 40+mark
或< 0-mark
。
mark += int(input("And the next mark? "))
# The next line is where the problem is
if mark < 0-mark or mark > 40+mark : # mark will never be > 40+mark or less than 0-mark
print("That is outside the score boundaries. Please enter the final three marks again.")
else:
break
这样做。
tmp = int(input("And the next mark? "))
if not (0 <= mark <= 40):
print("That is outside the score boundaries. Please enter the final three marks again.")
else:
mark += tmp
break