0

So I've been working on this code and got stuck and was wondering if you guys could help me out.

# 31/05/17 (dd/mm/yy)
# encryption and decryption
#--------------------------------------------
print("Hello, in this program you will be able to encrypt your own message, 
and decrypt others")
#--------------------------------------------
ans = int(input("""
What would you like to do:
1. Encrypt your own message
2. Decrypt a message
Type the number corresponding to the action that you want to preform, then 
press ENTER
"""))
#--------------------------------------------
 if ans == 1:
  print("hi") 

#--------------------------------------------
 else ans == 2:
  code = input("""
:""")
#-------------------------------------------- 
break

#--------------------------------------------

How would I make it where if they don't enter a 1 or a 2, so it will just re-ask them?

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

1

Create a while loop that will only break with a valid selection.

print("Please enter 1 or 2: ", end='')
while True:                        // always true
    choice = int(input()):
    if choice in (1, 2):
         break
    else:
        print("invalid entry, try again: ", end='')

if choice == 1:
    do somehting...
elif choice == 2:
    do something else...
syntaxError
  • 896
  • 8
  • 15
  • what does the end=' ' do? – Jordyn Wendling Jun 02 '17 at 00:10
  • The print statement usually defaults to end='\n', meaning at the end of a print statement it will start a new line by default in the terminal. By using end='' it means the print statement will not start a new line and instead ends with an empty string, so the input() statement be on the same line as the print statement, making it look nicer. – syntaxError Jun 02 '17 at 00:15
  • 1
    thanks for the help – Jordyn Wendling Jun 02 '17 at 00:17
0

Here is a simple change that will get you most of the way there:

print("Hello, in this program you will be able to encrypt your own message, and decrypt others")
#--------------------------------------------
ans = int(input("""
What would you like to do:
1. Encrypt your own message
2. Decrypt a message
Type the number corresponding to the action that you want to preform, then 
press ENTER
"""))
#--------------------------------------------

while True:
  #--------------------------------------------
  if ans == 1:
    print("hi")
    break
  #--------------------------------------------
  elif ans == 2:
    code = input("""
    :""")
    break
  #--------------------------------------------
  else:
    ans = int(input("""
    What would you like to do:
    1. Encrypt your own message
    2. Decrypt a message
    Type the number corresponding to the
    action that you want to preform, then press ENTER"""))
John Mulder
  • 9,765
  • 7
  • 33
  • 37
0

Try the bellow code

print("Hello, in this program you will be able to encrypt your own message,and decrypt others")
#--------------------------------------------
while True:
    try:ans = int(input("""
    What would you like to do:
    1. Encrypt your own message
    2. Decrypt a message
    Type the number corresponding to the action that you want to preform, then 
    press ENTER
    """))
    except:continue
    #--------------------------------------------
    if ans == 1:
      print("hi") 
      break
    #--------------------------------------------
    elif ans == 2:
      code = input("""
    :""")
      break

try except will help you re-ask when you enter non numbers too.

Mr. A
  • 1,221
  • 18
  • 28