Python 如果响应不是选项,如何循环?

Python 如果响应不是选项,如何循环?,python,python-3.x,Python,Python 3.x,所以我一直在写这段代码,我被卡住了,不知道你们能不能帮我一下 # 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") #------------------------

所以我一直在写这段代码,我被卡住了,不知道你们能不能帮我一下

# 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

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

如果他们没有输入1或2,那么我将如何使它位于何处,这样它只会重新询问他们?

创建一个while循环,该循环只会在有效选择的情况下中断

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

以下是一个简单的更改,它将让您在大部分时间内达到目标:

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"""))
试试下面的代码

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将帮助您在输入非数字时重新询问。

您是否尝试过编写while循环?我不知道要使用什么while循环。查看您尝试了什么会很有用,尽管end=''做了什么?print语句通常默认为end='\n',这意味着在打印语句结束时,它将在终端中默认开始一个新行。使用end=意味着print语句将不会开始新行,而是以空字符串结束,因此输入语句与print语句位于同一行,使其看起来更漂亮。感谢您的帮助这不是有NameError吗?是的,我跳过了前几行,因为它们没有更改。。。我会把它们加进去。输入部分的复制似乎是不必要的