python - 如何在 Python 中编写正确的 if 语句?
问题描述
我用 Python 制作了这个简单的 Caesar Cipher 程序,它对单词进行编码和解码,我要做的就是为用户写一个 if 语句,如果他们写了一个错误的单词(除了“解码”和“编码”之外的任何东西)并告诉他们再试一次,我一直在尝试并做了很多研究,但找不到任何帮助。当然还有很多其他方法可以做到这一点,但我想将其添加到我的特定代码中,而不是更改代码本身,我不知道如何..
任何帮助,将不胜感激..
这是我的代码:
#from art import logo
#print(logo)
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
if char in alphabet:
position = alphabet.index(char)
new_position = position + shift_amount
end_text += alphabet[new_position]
else:
end_text += char
print(f"\nHere's the {cipher_direction}d result: {end_text}")
should_continue = True
while should_continue:
direction = input("\nType 'encode' to encrypt, type 'decode' to decrypt: ")
text = input("\nType your message: ").lower()
shift = int(input("\nType the shift number: "))
shift = shift % 26
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
result = input("\nType 'yes' if you want to go again, otherwise type 'no': ").lower()
if result == "no":
should_continue = False
print("\nGoodbye!")
解决方案
direction = input("\nType 'encode' to encrypt, type 'decode' to decrypt: ").lower()
if direction != "encode" and direction != "decode":
continue
else:
text = ...
...
推荐阅读
- amazon-web-services - 使用 Chalice 向 AWS API Gateway 响应添加标头
- javascript - 如何让 owlCarousel 中的一张幻灯片比其他幻灯片停留的时间更长
- java - 等待函数完成,直到回调到来
- ssl - aks cert-manager 不使用lets-encrypt 创建证书
- python - 使用 keras 在 2D 数据上输出 1D 卷积的形状
- kubernetes - 嵌入式 Hazelcast 无法加入 Kubernetes 集群
- batch-file - 批处理文件中的多次测试 - REG QUERY
- jboss - 启动实例时让 DataVirt 运行
- three.js - 三.js新版99和morphanimation
- html - 带有 WebGL 1 的 16 位画布