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

标签 python

在我试图在这里编写的代码中,我需要它不断检查字符串的长度是否可以被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()

如果有一个简短而有效的答案,我们将不胜感激,谢谢!

最佳答案

在这里,我使用了从用户那里获取输入并检查是否满足条件。

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()

关于python - 有没有办法(永远)检查某个字符串的长度是否可以被某个数字整除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63992497/

相关文章:

python - 格式化 pandas 表对两列进行排序

python - 警告 - 找不到 Python 库,libdir=None、ldlibrary=None、multiarch=None、masd=None

python - 导入错误: undefined symbol: _PyUnicodeUCS4_IsWhitespace

python - 检查当前行中的所有列值是否小于 pandas 数据框中的所有先前行

python - 对 namedtuple 类进行自定义排序

python - if 部分的变量范围

python - 如何删除字符串中*在特定分隔符*内的特定字符*,例如括号内

python - 我的 for 循环没有根据条件删除数组中的项目? Python

python - 列组合计数,与顺序无关

mysql - Django 与 MySQL : Invalid utf8 character string: '800363' how to fix it?