Python:在while循环中添加边界值

Python:在while循环中添加边界值,python,Python,正如问题所说。我试图从一个学生那里得到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 bounda

正如问题所说。我试图从一个学生那里得到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")
为True时:
尝试:
mark=int(输入(“四分之一的第一分是什么?”)
如果标记<0或标记>40:
打印(“超出分数界限。请重试。”)
其他:
打破
除值错误外:
打印(“那不是数字!”)
计数器=1
当计数器40+标记时:
打印(“超出分数界限。请再次输入最后三分。”)
其他:
打破
除值错误外:
打印(“这不是数字!请再次输入最后三个标记。”)
计数器+=1
尽管如此:
尝试:
end_mark=int(输入(“您的年终分数是多少?”)
如果结束标记<0或结束标记>60:
打印(“超出分数界限。请重试。”)
其他:
打破
除值错误外:
打印(“那不是数字!”)
#计算
平均值=标记/计数器
总分=平均分+结束分
#加工
如果总分>50:
如果结束标记<30:
打印(“你有条件地以%{}通过考试。你的实际分数平均为{},但你的年终分数只有{}。”.format(总分,平均分,期末分数))
其他:
打印(“你以%{}通过。你的实际分数平均为{},年终分数为{}。”。格式(总分,平均分,期末分数))
其他:
打印(“很抱歉,你今年以%{}成绩不及格。你的实际成绩平均为{},年终成绩为{}。”。格式(总分,平均分,期末分))
打印(“代码结束”)

如何修复此问题?

这是因为您先添加然后检查,因此
标记
将永远不会是
>40+标记或
<0-标记

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
mark+=int(输入(“下一个标记是什么?”)
#下一行是问题所在

如果标记<0-标记或标记>40+标记:#标记永远不会>40+标记或小于0-标记 打印(“超出分数界限。请再次输入最后三分。”) 其他: 打破
改为这样做

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
tmp=int(输入(“下一个标记是什么?”)

如果不是(0有什么错误?“如果标记<0-标记或标记>40+标记:”(代码的第二段)。例如,如果我输入54,当我试图将边界设置为40时,它将接受该数字。
mark<0-mark
=>
mark<0
mark>40+mark
=>
False
。该条件意味着什么?注意:在pytrhon中,您可以使用这个条件的更好的书写方式:
(0谢谢,我发现这更容易理解,但是,“tmp”代表什么?
tmp
只是一个临时变量,用于存储输入值,直到我们检查它是否在范围内。如果这对您有帮助,请将其标记为答案。