问题在于,当您输入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:
答案 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
如果,您的程序将会更干净
5*days_late
,然后将其减去,然后在需要时将其设置回40。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)