X-5 = Y。当达到40时关闭程序

时间:2018-11-08 20:01:52

标签: python

问题在于,当您输入40时,它会下降到35,如果您输入数字46、48、57、78等也会发生这种情况。

def Marking():
    Mark=int(input("What mark did you get (between 0 - 100)?: "))
    while Mark >100:
        Mark=int(input("What mark did you get (between 0 - 100)?: "))

    days_late=int(input("How many days late was it submited (between 0 - 12)? :"))
    while days_late >12:
        days_late=int(input("How many days late was it submited (between 0 - 12)? :"))

    while days_late != 0:
        Mark = Mark - 5
        days_late = days_late -1
        print(days_late, "Final mark is =", Mark)

        if Mark <=40:
            break       #The problem is somewhere here i think 

    while days_late == 0:
        print("your final mark is: ", Mark)
        break



    while 0 <= Mark <=40:
            print("your mark is:", Mark)
            break

What mark did you get (between 0 - 100)?: 40

How many days late was it submited (between 0 - 12)? :5

4 Final mark is = 35         ########this should be capped at 40 and no lower

A. Marking
E. Extend
X. Exit Program
Enter an Option: A,E or X:

3 个答案:

答案 0 :(得分:1)

if Mark <=40: ... break会立即中断循环,如果您输入的数值等于或大于40(加上减去的5),那么最多只能打印40张(例如,从45开始) ,然后减去5,然后打破循环)。

不相关,但是如果输入负数days_late,则有一个无限循环while days_late != 0,因此建议您先设置while days_late > 0,然后再选择mark = mark - 5。除非您真的想限制标记值可以下降的程度,否则根本不需要break语句。

然后丢失最后两个while循环,请改用if。而且我认为您不需要两次打印相同的标记值


总而言之,应该确实是这样

while days_late > 0:
    mark -= 5
    days_late -= 1
    print(days_late, "Current mark is =", mark)

print("your final mark is: ", mark)

如果您需要将最大标记限制为40,则可以在减去5之后执行此操作

mark = max(mark, 40)

答案 1 :(得分:0)

是的,问题在这里:

if Mark <=40:
    break       #The problem is somewhere here i think

您离开循环知道标记可能在35-40范围内的任何位置。请注意,只有在获得5分后,您才可以检查该值。然后,您break的价值将您带入40。

在离开前进行调整:

if Mark <=40:
    Mark = 40
    break

如果,您的程序将会更干净

  1. 只有在最终标记最终之后才打印最终标记;不要每次都循环打印。
  2. 更好的是,不要循环播放。只需将扣减计算为5*days_late,然后将其减去,然后在需要时将其设置回40。
  3. 删除最后的while-只需到达此位置,您就可以知道已执行了罚款。 是您打印最终成绩的地方。

答案 2 :(得分:0)

递减完成后,您正在崩溃。在40个极限过滤器之后递减如何?

while days_late != 0:

    print(days_late, "Final mark is =", Mark)

    if Mark <=40:
        break       

    Mark = Mark - 5

    days_late = days_late - 1

输出:

What mark did you get (between 0 - 100)?: 50
How many days late was it submited (between 0 - 12)? :12
(12, 'Final mark is =', 50)
(11, 'Final mark is =', 45)
(10, 'Final mark is =', 40)
('your mark is:', 40)