2016-03-28 9 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 

しかし、それは最終的な結果は、クリーンで読みやすくなりますので、機能にコードを分割することです行うための正しい方法の後。 APersonのソリューションは正常に動作します@タイトル行の印刷後

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

を開始します。やってみて。プログラムは唯一、最終的な暗号化された結果を表示するよう

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

はまた、forループの外に print(translated)を移動することを検討します。

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) 
関連する問題