Skip to main content
 首页 » 编程设计

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

2025年05月04日10linjiqin

我的程序要求用户输入,但我想拒绝任何 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