首页 文章

如何避免使用基数为10的'ValueError:int()的无效文字:''“?

提问于
浏览
-1

我一直致力于一个旨在加密用户输入的消息的程序 . 加密过程完成后,我希望程序提示用户选择是否要加密另一条消息 .

option2 = int(input('Would you like to encrypt another message? (Yes = 1 and No = 2)'))
    while option2 not in [1, 2]:

        print 'Please type 1 or 2.'
        option2 = int(raw_input())
    while True:
        option2 = int(raw_input())
        if option2 == 1:
            option1 = int(input('Which encryption method would you like to use? 1 = Across (NOPQ ...) and 2 = Backwards (ZYXW ...)'))
    while True:
        option2 = int(raw_input())
        if option2 == 2:
            break

此代码导致

“ValueError:基数为10的int()的无效文字:''”

我以前从未遇到的错误 . 我该如何解决?

1 回答

  • 1

    问题是当你这样做时,你试图在第一行转换为int:

    int(input(...
    

    将输入存储在字符串中,检查

    option2 not in ['1', '2']
    

    那部分应该有效 .

    请考虑在此处查看答案,了解有关如何改进菜单的提示:Creating a Menu in Python

相关问题