python - 如果输入是 x 的倍数,如何采取不同的行动?

标签 python

我的程序要求用户输入,但我想拒绝任何 26 的倍数(或者如果输入 0)并要求用户再次输入。我不知道该怎么做;我猜这与将输入除以 26 并得到一个整数有关。

当前代码为:

  ValidInput = False  
  while ValidInput == False: 
    try: 
      Key = int(input('Enter the amount that shifts the plaintext alphabet to the ciphertext alphabet: ')) 
    except: 
      print("Sorry, that isn't an integer. ") 
    else: 
      ValidInput = True 
  return Key

最佳答案

您可以使用模运算符:

if Key % 26 == 0:  # If Key / 26 returns no remainder
    # Key is therefore divisible by 26

关于python - 如果输入是 x 的倍数,如何采取不同的行动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290316/

相关文章:

java - 模拟器框架

python - 随机森林的可解释性

python - 在Python中分解字符串

python - 使用Python获取环境变量地址

python - 将项目推到堆栈的末尾(第 2 部分)

python Spark根据值对元素进行排序

python - 在 python 中应用模数列表的最惯用方法

python - 如何在 moviepy 中创建包含多个文本的 textClip?

python - Python 音乐播放器无法正常工作

python - 如何使用 python 从嘈杂的文件中解析出 xml