为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法(永远)检查某个字符串的长度是否可以被某个数字整除?

有没有办法(永远)检查某个字符串的长度是否可以被某个数字整除?

慕妹3242003 2023-08-22 15:23:26
在我试图在这里编写的代码中,我需要它不断检查字符串的长度是否能被 3 整除,如果是,则输出有效输出,如果不是,则再次循环。我事先在代码中定义了所有其他变量,例如 a、u()。def u():  if int(len(str(a))) ('is divisible by 3'):    print('The sum of all characters in that string is divisible by 3')  while int(len(str(a)))  ('is not divisible by 3'):    print('The sum of all characters in that string is NOT divisible by 3, try again!')    u()u()一个简短而有效的答案将不胜感激,谢谢!
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

在这里,我使用了来自用户的输入并检查是否满足条件。


def u():


  a = input(' Input string ')

  if int(len(str(a)))%3 ==0:   #('is divisible by 3'):

      print('The sum of all characters in that string is divisible by 3')

  else: # int(len(str(a)))  ('is not divisible by 3'):

      print('The sum of all characters in that string is NOT divisible by 3, try again!')

      u()

u()


查看完整回答
反对 回复 2023-08-22
  • 1 回答
  • 0 关注
  • 1570 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信