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

kei*_*tre 1 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
Run Code Online (Sandbox Code Playgroud)

Ble*_*der 8

您可以使用模运算符:

if Key % 26 == 0:  # If Key / 26 returns no remainder
    # Key is therefore divisible by 26
Run Code Online (Sandbox Code Playgroud)

  • 并且,方便地,`0%26 == 0`所以当输入为0时它也做正确的事!:-) (2认同)