Home > Net >  How to write code which return to the first step in python
How to write code which return to the first step in python

Time:04-23

input:

val=input('Do you want to encrypt or decrypt a message or quit: ')
if val=="encrypt":
    enr=input('Please enter a string: ')
    for e in enr:
      if "a"<=e<="z":
          print(chr(ord("a") (ord(e)-ord("a") 3)&),end="")
      elif "A"<=e<="Z":
          print(chr(ord("A") (ord(e)-ord("A") 3)&),end="")
      else:
          print(e,end="")


if val=="decrypt":
     dec=input('Please enter a string: ')
     for d in dec:
         if "a"<=d<="z":
          print(chr(ord("a") (ord(d)-ord("a")-3)&),end="")
         elif "A"<=d<="Z":
          print(chr(ord("A") (ord(d)-ord("A")-3)&),end="")
         else:
             print(d,end="")
            
if val=="quit":
        print('Thank you for using')

After print out the encrypt or decrypt message, go back to the first input question: val=input('Do you want to encrypt or decrypt a message or quit: ') if quit then stop the loop

CodePudding user response:

Put your encrypt/decrypt code into a function; then you can easily call it in a loop that you break when it's time to quit.

def shift_chars(msg: str, shift: int) -> str:
    def shift_char(c: str) -> str:
        if "a" <= c <= "z":
            return chr(ord("a")   (ord(c) - ord("a")   shift) % 26)
        elif "A" <= c <= "Z":
            return chr(ord("A")   (ord(c) - ord("A")   shift) % 26)
        else:
            return c
    return ''.join(shift_char(c) for c in msg)

while True:
    val = input('Do you want to encrypt or decrypt a message or quit: ')
    if val == "encrypt":
        shift = 3
    elif val == "decrypt":
        shift = -3
    elif val == "quit":
        print('Thank you for using')
        break
    else:
        continue
    print(shift_chars(input('Please enter a string: '), shift))

CodePudding user response:

This approach should help:

We start off by gathering an input from the user using your input() function.

Then we put the other code into a while loop that checks the value of val at every cycle. In addition, we add a line at the end of the while loop to ask the user for whether they want to encrypt/decrypt OR quit. This updates the current value of val to trigger the while loop to stop, if necessary:

val=input('Do you want to encrypt or decrypt a message or quit: ')

while val != 'quit':
    if val=="encrypt":
        enr=input('Please enter a string: ')
        for e in enr:
          if "a"<=e<="z":
              print(chr(ord("a") (ord(e)-ord("a") 3)&),end="")
          elif "A"<=e<="Z":
              print(chr(ord("A") (ord(e)-ord("A") 3)&),end="")
          else:
              print(e,end="")


    if val=="decrypt":
         dec=input('Please enter a string: ')
         for d in dec:
             if "a"<=d<="z":
              print(chr(ord("a") (ord(d)-ord("a")-3)&),end="")
             elif "A"<=d<="Z":
              print(chr(ord("A") (ord(d)-ord("A")-3)&),end="")
             else:
                 print(d,end="")
    val=input('Do you want to encrypt or decrypt a message or quit: ') 

print('Thank you for using')

A slightly cleaner version

As was noted in some of the comments/answers, it may be useful to encapsulate some of the code into a function to make the while loop a bit cleaner.

Below is an approach to doing this that stays fairly true to your original code. We add an encryptor/decryptor function so that we can reduce some of the duplicate code. We determine whether the shift should be up OR down by 3 OR -3. Then we use that shift_value to change the value for the character.

def enc_decryptor(transformation):
    # Determine whether to shift up OR down
    if transformation == 'encrypt':
        shift_value = 3
    elif transformation == 'decrypt':
        shift_value = -3
    
    # Perform the transformation
    msg=input('Please enter a string: ')
    for char in msg:
      if "a" <= char <= "z":
          print(chr(ord("a") (ord(char)-ord("a")   shift_value)&),end="")
      elif "A" <= char <= "Z":
          print(chr(ord("A") (ord(char)-ord("A")   shift_value)&),end="")
      else:
          print(char, end="")

We need to modify our while loop to account for the new code and we can see that the while loop is now much simpler.

val=input('Do you want to encrypt or decrypt a message or quit: ')

while val != 'quit':
    if val=="encrypt":
        enc_decryptor(transformation=val)


    if val=="decrypt":
         enc_decryptor(transformation=val)
            
    val=input('Do you want to encrypt or decrypt a message or quit: ') 

print('Thank you for using')
  • Related