Python:凯撒密码,If语句和else语句同时为True?
我几乎已经解决了这个问题,但由于某种原因,第一个 If 语句和 else 语句对于任何大写字母输入都为 true。因此,如果 ASCII 值介于 65 和 90 之间,则 if 语句声明 true 并打印出该值,但 else 语句也声明 true 并打印出一条语句。如果我将“继续”放在第一个 if 语句的底部,则该程序可以完美运行。但是我不知道为什么它会这样工作。你能帮我纠正我的逻辑缺陷吗,我不明白为什么要这样做。这是我的代码和输出:
k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext: ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array
for i in range(0,len(original)): ##Now seperating each character to add k
char = original_as_array[i]
charint = ord(char)
if charint >= 65 and charint <=90:
cipher_int = ((charint-65 + k) % 26)+65
code_char = chr(cipher_int)
print code_char,
if charint >= 97 and charint <=122:
cipher_int = ((charint-97 + k) % 26)+97
code_char = chr(cipher_int)
print code_char,
else:
print char,
仅包含大写字母的示例输出:
Please enter a value for k: 13
plaintext: PLEASE HELP
C P Y L R E N A F S R E U H R E Y L C P
I pretty much have this solved but for some reason the first If statement and the else statement are both saying true for any capitalized letter inputs. So if the ASCII value lands between 65 and 90 the if statement declares true and prints out that value, but then the else statement also declares true and prints out a statement. If I put 'continue' at the bottom of the first if statement, this program works flawlessly. However I have no clue why it works like that. Can you please help me correct my flaw in logic, I dont understand why it is doing this. Here is my code and output:
k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext: ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array
for i in range(0,len(original)): ##Now seperating each character to add k
char = original_as_array[i]
charint = ord(char)
if charint >= 65 and charint <=90:
cipher_int = ((charint-65 + k) % 26)+65
code_char = chr(cipher_int)
print code_char,
if charint >= 97 and charint <=122:
cipher_int = ((charint-97 + k) % 26)+97
code_char = chr(cipher_int)
print code_char,
else:
print char,
Example output with only caps:
Please enter a value for k: 13
plaintext: PLEASE HELP
C P Y L R E N A F S R E U H R E Y L C P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的第一个 if 语句与 else 语句无关。您希望
“否则”(对于大写字母)第一个条件解析为 true,第二个条件解析为 false,并且由于该语句解析为 false,因此执行 else 语句。
Your first if statement isn't related to the else statement. You want
Otherwise (for capital letters) the first condition resolves to true, the second to false, and because the statement resolved to false the else statement gets executed.
您还应该学习如何使您的代码更加Pythonic。
首先,Python 列表不是数组,它们是列表。不是同一件事。
您不需要从字符串中创建列表。 Python 字符串是字符序列,并且已经支持订阅:
但是您既不需要列表也不需要订阅来访问字符串的字符。您可以而且应该像这样进行迭代:
此外,Python 中的比较运算符可以而且应该链接起来:
存在重复。不要重复自己:
You should also learn how to make your code more pythonic.
First things first: Python lists are not arrays, they are lists. Not the same thing.
You don't need to crate a list from your string. Python strings are sequences of characters and already support subscription:
But you need neither lists nor subscriptions to access the characters of your string. You can, and should, iterate like this:
Also, comparison operators in Python can, and should, be chained:
There's repetition going on. Don't repeat yourself:
您的问题是您需要使用 elif (请参阅 http://docs.python.org/tutorial/ controlflow.html)。
如果字母不是小写,则 else: 子句运行。
顺便说一句,您不需要将“原始”列表。 python 中的字符串与列表的行为几乎相同。
k = int(raw_input("请输入 k 的值:")) #移位器编号
original = raw_input("plaintext: ") #用户想要加密的消息
Original_as_array = list(original) ##我将输入转换为数组
for i in range(0,len(original)): ##现在分隔每个字符以添加 k
字符 = 原始数组[i]
charint = ord(char)
Your problem is that you need to use elif (see http://docs.python.org/tutorial/controlflow.html).
The else: clause runs if the letter isn't lower case.
By the way, you don't need to make the "original" a list. Strings in pythons behave pretty much identically to lists.
k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext: ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array
for i in range(0,len(original)): ##Now seperating each character to add k
char = original_as_array[i]
charint = ord(char)
试试这个:
在每个成功的案例之后,您需要使用
继续
跳过其他案例。附注:
也可以按如下方式完成:
Try this:
After every successful case, you need to skip the other cases with
continue
.As a side note:
This can also be done as follows: