首页 > 解决方案 > 如果数字不是 10.000 触发错误建议的倍数,如何使用条件创建验证?

问题描述

我为现金箱退休金 ATM 制作了一个算法,如果询问退休值并且如果它不是 10.000 的倍数,我需要触发一个建议:

Type a valid, value {value} is not accepted. In the case that the typed value is not multiple of 10k

我的解决方案是创建一个函数,其中退休值除以 10.000。如果除法结果给出浮点结果,则会触发错误建议。

def multiple10k_validation(inserted_value):
validation_result = inserted value/10000
if alidation_result ==
retunr f Type a valid, value {value} is not accepted. In the case that the typed value is not 
multiple of 10k

print(multiple10k(1550000))

有人建议我单独创建一个模块来验证票据的价值。但不知道该怎么做。谢谢

标签: python

解决方案


这个想法很好,但你不应该使用除法。而是使用模运算符%。它返回除法的余数,例如9 % 4 = 2 remainder 1。另请参阅有关操作员的本指南,这可能有助于理解它。

因此,在您的代码中,您将在应用模 10000 时检查余数 0:

def multiple10k_validation(inserted_value):
    if inserted_value % 10000 != 0:
        return "Type a valid, value {value} is not accepted. In the case that the typed value is not multiple of 10k"
    
    return "Valid value"

print(multiple10k_validation(1550000))

推荐阅读