亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Python 3.8中將不同行上的字符打印到字符串中

在Python 3.8中將不同行上的字符打印到字符串中

30秒到達戰場 2023-08-08 17:02:42
這是我的代碼,目前它在單獨的行上打印解密的凱撒密碼字符。有什么方法可以將它們作為字符串添加到一行上嗎?此外,是否有一種可能的方法來實現 .isalpha() 來解釋未加密消息中的空格和問號等。"""Cypher program."""import stringalphabet = string.ascii_lowercasemessage = "thequickbrownfoxjumpsoverthelazydog"key = 7for char in message:    new_char = key + (alphabet.index(char))    if new_char > 25:        new_char = new_char % 26    print(alphabet[new_char])我對 Python 很陌生,如果這是一個新手問題,我很抱歉。非常感謝任何好心人的幫助。
查看完整描述

2 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

您可以將alphabet[new_char]追加到列表中,然后使用 join 將其打印為字符串。下面的示例代碼(經過編輯以讓非字母數字的字符保留在原處):


import string


alphabet = string.ascii_lowercase

message = "the quick brow???nxa2 fox jumps over the lazy dog"

key = 7

lst=[]

for char in message:

    if char.isalpha() is True:

        new_char = key + (alphabet.index(char))

        if new_char > 25:

            new_char = new_char % 26

        lst.append(alphabet[new_char])

    else:

        lst.append(char)

print(''.join(i for i in lst))


查看完整回答
反對 回復 2023-08-08
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

"""Cypher program."""

import string


alphabet = string.ascii_lowercase

message = "thequick0brownfox jumpsoverthelazydog"


def transform(char,key):

    if char.isalpha():

       new_char = key + (alphabet.index(char))

       if new_char > 25:

           new_char = new_char % 26

       return alphabet[new_char]

    return char


key = 7


# faster string comprehension

decripted = [transform(char,key) for char in message]

  

print(decripted)

# or 


# "".join - puts all elements of an array toghether in a string using a separator

print("".join(decripted))


查看完整回答
反對 回復 2023-08-08
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號