2016-03-28 98 views
0

我在Python中使用了這個凱撒的密碼代碼來快速加密一些消息,並將其展示給我的同學。如何在Python中循環代碼?

我已經完成這一切,除了東西...

我想打一個「你要加密的另一個消息?」選項,但我無法循環代碼。

如何循環整個代碼?我正在使用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.') 
+0

把代碼放在問題中,而不是鏈接中。要使其成爲代碼塊,請突出顯示它並按下Ctrl-k。 – zondo

回答

1

一種方法是使用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.") 
+0

這不是太簡單。 「是」將被視爲是。嘗試一下。 –

+0

@zondo我修好了。 – APerson

0

最簡單的方法是將整個代碼放入'while running'循環中,並在循環結束時詢問該人是否要再次運行代碼,如果不是,則將運行更改爲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 

但要做到這將是你的代碼分爲功能,所以最終的結果將是更清潔和更容易閱讀的正確方法。

0

標題行的打印後啓動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') 
0

@ 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)到的外部for循環所以程序只顯示最終加密結果。

0

把這個代碼代碼上面:

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") 

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

0
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)