Python如何使它输入没有输入打印输入失败

时间:2015-12-09 20:55:00

标签: python python-3.x

当我按输入中的输入时,我如何制作它以便我的程序打印“你没有输入等等等等等”这是我的加密程序。

answer="y" #set answer as a variable
while answer=="y" or answer=="yes":

def encryption(plaintext, keyword):
   #pointless including my functions for this question

def decryption(plaintext, keyword):       


question = input("Would you like to encrypt or decrypt a message? ")

if question == 'e' or question == 'encrypt':
    plaintext=input("Please enter the message you wish to encode: ").lower()#plaintext message wished to be encrypted
    keyword=input("Please enter your keyword: ").lower()#keyword that encrypts the message
    encryption(plaintext, keyword)#calls encryption function

elif question == 'd' or question == 'decrypt':
    plaintext=input("Please enter the encrypted message you wish to decrypt: ").lower()
    keyword=input("Please enter your keyword: ").lower()
    decryption(plaintext, keyword)

answer=input("Would you like to restart the program? ").lower()
print("Goodbye")

我尝试使用try:但是:但是这似乎没有用,或者我只是错误地使用了它?

1 个答案:

答案 0 :(得分:0)

编辑或验证您需要使用while循环的输入:

while True:
    ans = input('Prompt ')
    if ans:
        break
    print ('You need to type something')

你还应该在休息之前检查有效值

尝试除了仅用于例外 - 不输入任何东西不是错误它只返回一个空字符串''