Python:凯撒密码,If语句和else语句同时为True?

发布于 2024-11-02 20:46:01 字数 1078 浏览 1 评论 0原文

我几乎已经解决了这个问题,但由于某种原因,第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假情假意假温柔 2024-11-09 20:46:01

您的第一个 if 语句与 else 语句无关。您希望

if charint >= 65 and charint <=90:
        cipher_int = ((charint-65 + k) % 26)+65
        code_char = chr(cipher_int)
        print code_char,

elif charint >= 97 and charint <=122:
    cipher_int = ((charint-97 + k) % 26)+97
    code_char = chr(cipher_int)
    print code_char,


else:
    print char,

“否则”(对于大写字母)第一个条件解析为 true,第二个条件解析为 false,并且由于该语句解析为 false,因此执行 else 语句。

Your first if statement isn't related to the else statement. You want

if charint >= 65 and charint <=90:
        cipher_int = ((charint-65 + k) % 26)+65
        code_char = chr(cipher_int)
        print code_char,

elif charint >= 97 and charint <=122:
    cipher_int = ((charint-97 + k) % 26)+97
    code_char = chr(cipher_int)
    print code_char,


else:
    print char,

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.

凉墨 2024-11-09 20:46:01

您还应该学习如何使您的代码更加Pythonic。

首先,Python 列表不是数组,它们是列表。不是同一件事。

您不需要从字符串中创建列表。 Python 字符串是字符序列,并且已经支持订阅:

>>> 'hello'[4]
'o'

但是您既不需要列表也不需要订阅来访问字符串的字符。您可以而且应该像这样进行迭代:

for char in original:
    ...

此外,Python 中的比较运算符可以而且应该链接起来:

if 65 <= charint <= 90:
    ...

存在重复。不要重复自己:

def cipher(ch_int, offset, lowest):
    return chr((ch_int - lowest + offset) % 26 + lowest)

for char in original:
    charint = ord(char)
    if 65 <= charint <= 90:
        print cipher(charint, k, 65),
    elif 97 <= charint <= 122:
        print cipher(charint, k, 97),
    else:
        print char,

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:

>>> 'hello'[4]
'o'

But you need neither lists nor subscriptions to access the characters of your string. You can, and should, iterate like this:

for char in original:
    ...

Also, comparison operators in Python can, and should, be chained:

if 65 <= charint <= 90:
    ...

There's repetition going on. Don't repeat yourself:

def cipher(ch_int, offset, lowest):
    return chr((ch_int - lowest + offset) % 26 + lowest)

for char in original:
    charint = ord(char)
    if 65 <= charint <= 90:
        print cipher(charint, k, 65),
    elif 97 <= charint <= 122:
        print cipher(charint, k, 97),
    else:
        print char,
朕就是辣么酷 2024-11-09 20:46:01

您的问题是您需要使用 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)

if charint >= 65 and charint <=90:
    cipher_int = ((charint-65 + k) % 26)+65
    code_char = chr(cipher_int)
    print code_char,

elif charint >= 97 and charint <=122:
    cipher_int = ((charint-97 + k) % 26)+97
    code_char = chr(cipher_int)
    print code_char,


else:
    print 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)

if charint >= 65 and charint <=90:
    cipher_int = ((charint-65 + k) % 26)+65
    code_char = chr(cipher_int)
    print code_char,

elif charint >= 97 and charint <=122:
    cipher_int = ((charint-97 + k) % 26)+97
    code_char = chr(cipher_int)
    print code_char,


else:
    print char,
相权↑美人 2024-11-09 20:46:01

试试这个:

#!/usr/bin/python

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,
        continue

    if charint >= 97 and charint <=122:
        cipher_int = ((charint-97 + k) % 26)+97
        code_char = chr(cipher_int)
        print code_char,
        continue

    print char,

在每个成功的案例之后,您需要使用继续跳过其他案例。

附注:

也可以按如下方式完成:

>>> import string
>>> transtab = string.maketrans(string.lowercase + string.uppercase,string.lowercase[k:] + string.lowercase[:k] + string.uppercase[k:] + string.uppercase[:k])
>>> "PLEASE HELP".translate(t)
'CYRNFR URYC'

Try this:

#!/usr/bin/python

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,
        continue

    if charint >= 97 and charint <=122:
        cipher_int = ((charint-97 + k) % 26)+97
        code_char = chr(cipher_int)
        print code_char,
        continue

    print char,

After every successful case, you need to skip the other cases with continue.

As a side note:

This can also be done as follows:

>>> import string
>>> transtab = string.maketrans(string.lowercase + string.uppercase,string.lowercase[k:] + string.lowercase[:k] + string.uppercase[k:] + string.uppercase[:k])
>>> "PLEASE HELP".translate(t)
'CYRNFR URYC'
莫言歌 2024-11-09 20:46:01
word=raw_input("ENTER YOUR MESSAGE IN CAPITAL LETTERS :")

def cal(a):
    if a=="A":
        c="D"
    elif a=="B":
        c="E"
    elif a=="C":
        c="F"
    elif a=="D":
        c="G"
    elif a=="E":
        c="H"
    elif a=="F":
        c="I"
    elif a=="G":
        c="J"
    elif a=="H":
        c="K"
    elif a=="I":
        c="L"
    elif a=="J":
        c="M"
    elif a=="K":
        c="N"
    elif a=="L":
        c="O"
    elif a=="M":
        c="P"
    elif a=="N":
        c="Q"
    elif a=="O":
        c="R"
    elif a=="P":
        c="S"
    elif a=="Q":
        c="T"
    elif a=="R":
        c="U"
    elif a=="S":
        c="V"
    elif a=="T":
        c="W"
    elif a=="U":
        c="X"
    elif a=="V":
        c="Y"
    elif a=="W":
        c="Z"
    elif a=="X":
        c="A"
    elif a=="Y":
        c="B"

    elif a=="Z":
        c="C"
    elif a==" ":
        c=" "
    else:
        c=a
    return c

b=len(word)
l=""
a=0
while a<b:
    l=l+cal(word[a])
    a=a+1

print l
word=raw_input("ENTER YOUR MESSAGE IN CAPITAL LETTERS :")

def cal(a):
    if a=="A":
        c="D"
    elif a=="B":
        c="E"
    elif a=="C":
        c="F"
    elif a=="D":
        c="G"
    elif a=="E":
        c="H"
    elif a=="F":
        c="I"
    elif a=="G":
        c="J"
    elif a=="H":
        c="K"
    elif a=="I":
        c="L"
    elif a=="J":
        c="M"
    elif a=="K":
        c="N"
    elif a=="L":
        c="O"
    elif a=="M":
        c="P"
    elif a=="N":
        c="Q"
    elif a=="O":
        c="R"
    elif a=="P":
        c="S"
    elif a=="Q":
        c="T"
    elif a=="R":
        c="U"
    elif a=="S":
        c="V"
    elif a=="T":
        c="W"
    elif a=="U":
        c="X"
    elif a=="V":
        c="Y"
    elif a=="W":
        c="Z"
    elif a=="X":
        c="A"
    elif a=="Y":
        c="B"

    elif a=="Z":
        c="C"
    elif a==" ":
        c=" "
    else:
        c=a
    return c

b=len(word)
l=""
a=0
while a<b:
    l=l+cal(word[a])
    a=a+1

print l
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文