2016-03-28 71 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)