2016-03-28 6 views
0

Ich habe diesen Caesar's Cipher-Code in Python, um einige Nachrichten schnell zu verschlüsseln, und zeige es meinen Klassenkameraden.Wie man einen Code in Python wiederholt?

Ich habe alles getan, außer etwas ...

Ich möchte ein machen ‚Möchten Sie eine andere Nachricht verschlüsseln möchten?‘ Option, aber ich kann den Code nicht loopen.

Wie kann ich den gesamten Code loopen? Ich benutze Python 3.5.1.

Hier ist mein Code:

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

Setzen Sie den Code in der Frage, nicht in einem Link. Um einen Codeblock zu erstellen, markieren Sie ihn und drücken Sie Strg-k. – zondo

Antwort

1

Eine Möglichkeit, es wird mit einer while-Schleife zu tun, die für immer geht weiter (bis Sie aus ihm heraus zu brechen):

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

Es ist nicht zu einfach. "Ja" würde als Ja zählen. Versuch es. –

+0

@zondo Ich habe es behoben. – APerson

0

Der einfachste Weg wäre, Um den gesamten Code in eine "while running" -Schleife einzufügen, fragen Sie am Ende der Schleife, ob die Person den Code erneut ausführen möchte. Wenn nicht, ändern Sie running auf False.

Vor

print("Hello World!") 

Nach

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

Aber der richtige Weg, es zu tun wäre, um Ihren Code in Funktionen zu teilen, so würde das Endergebnis sauberer und leichter zu lesen.

0

Nach dem Druck der Titelzeile starten die while-Schleife

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 Lösung funktioniert gut. Versuch es.

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

berücksichtigen print(translated) nach außerhalb der for-Schleife zu bewegen, so dass nur das Programm das letzte verschlüsselte Ergebnis anzeigt.

0

setzen diesen Code über Ihren Code:

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

das obige Verfahren eine einfache und kleine Änderungen an Ihrem vorhandenen Code benötigt. Ich hoffe es hilft!

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) 
Verwandte Themen