python - 如果数字不是 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))
有人建议我单独创建一个模块来验证票据的价值。但不知道该怎么做。谢谢
解决方案
这个想法很好,但你不应该使用除法。而是使用模运算符%
。它返回除法的余数,例如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))
推荐阅读
- java - 使用 Spring Boot 运行 Vue
- javascript - 异步函数中的多个等待不返回
- python - Groupby 具有产品聚合的多列
- presto - 在 AWS Athena 上使用 Presto 的 Coalesce 函数和一行
- apache - 仅允许通过 .htaccess 的 cloudflare ip 范围给出 403 错误
- ios - 找不到“AppsFlyerLib/AppsFlyerTracker.h”文件,有人见过这个错误吗?
- javascript - DJango React 未找到
- java - 从elasticsearch连接并读取数据到hive
- javascript - 试图更改 h1 标签的内部 html 但 js 抛出错误
- flutter - Flutter IconData 预览不显示在 VSCode 中