簡體   English   中英

如何在Python中循環編碼?

[英]How to loop a code in Python?

我用Python編寫了這個Caesar的Cipher代碼,可以快速加密一些消息,並將其顯示給我的同學。

我已經做好了,除了某些事情...

我要輸入“是否要加密另一封郵件?” 選項,但是我無法循環代碼。

如何循環整個代碼? 我正在使用Python 3.5.1。

這是我的代碼:

print('QuantumShadow\'s Caesar Cipher')
message = input('Write your message here: ')
print('The encryption key is: ')
key = int(input())
print('Do you want to encrypt or decrypt?')
mode = input()
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
translated = ''
message = message.upper()
for symbol in message:
    if symbol in LETTERS:
        num = LETTERS.find(symbol)
        if mode == 'encrypt':
            num = num + key
        elif mode == 'decrypt':
            num = num - key

        if num >= len(LETTERS):
            num = num - len(LETTERS)
        elif num < 0:
            num = num + len(LETTERS)
        translated = translated + LETTERS[num]
    else:
        translated = translated + symbol
    print(translated)
print('Do you want to encrypt\\decrypt another message?')
print('Here is where I want to make the loop')
print('Coded with Python by QuantumShadow.')

一種方法是使用永遠持續的while循環(直到退出該循環):

while True:
    # The rest of your code
    if not input("Do you want to encrypt or decrypt another message [y/n]? ").lower().startswith("y"):
        break
print("Coded with Python by QuantumShadow.")

最簡單的方法是將整個代碼放入“運行時”循環中,並在循環結束時詢問用戶是否要再次運行該代碼,如果不想,則將運行更改為False。

之前

print("Hello World!")

running = True
while running:
    print("Hello World!")
    answer = input("Would you like to run again? (y/N)")
    if answer != 'y':
        running = False

但是正確的方法是將代碼分成函數,因此最終結果將更清晰,更易於閱讀。

打印標題行后,開始while循環

ask = True
while ask: # this was initialized to True
    message = input('Write your message here: ')
    key = int(input('The encryption key is: '))
    mode = input('Do you want to encrypt or decrypt?')

    # put the coding logic here
    next = input('Do you want to encrypt\\decrypt another message?')
    if next.lower() == 'no':
        ask = False
print('You have finished all messages')

@APerson的解決方案工作正常。 試試看。

while True:
    print('QuantumShadow\'s Caesar Cipher')
    message = input('Write your message here: ')
    print('The encryption key is: ')
    key = int(input())
    print('Do you want to encrypt or decrypt?')
    mode = input()
    LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    translated = ''
    message = message.upper()
    for symbol in message:
        if symbol in LETTERS:
            num = LETTERS.find(symbol)
            if mode == 'encrypt':
                num = num + key
            elif mode == 'decrypt':
                num = num - key

            if num >= len(LETTERS):
                num = num - len(LETTERS)
            elif num < 0:
                num = num + len(LETTERS)
            translated = translated + LETTERS[num]
        else:
            translated = translated + symbol
        print(translated)
    if input("Do you want to encrypt or decrypt another message [yes/no]? ") != "yes":
        break
print("Coded with Python by QuantumShadow.")

還可以考慮將print(translated)print(translated)移到for循環之外,以便程序僅顯示最終的加密結果。

將此代碼放在代碼上方:

x=1
while x==1:
    Your Code after indent
    #Add the following lines below
    x=input("Press  to send another message or any other key to exit")

上面的方法很簡單,幾乎不需要修改現有代碼。 希望能幫助到你!

print('QuantumShadow\'s Caesar Cipher')
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

#Wrap your code
def get_message(): 
    message = input('Write your message here: (input q to quit)')
    if message == 'q':
        return message
    print('The encryption key is: ')
    key = int(input())
    print('Do you want to encrypt or decrypt?')
    mode = input()
    translated = '' 
    message = message.upper()  
    for symbol in message:
        if symbol in LETTERS:
            num = LETTERS.find(symbol)
            if mode == 'encrypt':
                num = num + key
            elif mode == 'decrypt':
                num = num - key

            if num >= len(LETTERS):
                num = num - len(LETTERS)
            elif num < 0:
                num = num + len(LETTERS)
            translated = translated + LETTERS[num]
        else:
            translated = translated + symbol
    return translated

#loop your code
while True:
    message = get_message()
    if message == 'q':
        break
    print (message)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM