【问题标题】:How can I avoid "ValueError: invalid literal for int() with base 10: ''"?如何避免“ValueError:int() 以 10 为基数的无效文字:''”?
【发布时间】:2018-05-03 04:40:46
【问题描述】:

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

    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: int() 以 10 为底的无效文字:''"

我从未遇到过的错误。我该如何解决这个问题?

【问题讨论】:

标签: python int literals base valueerror


【解决方案1】:

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

int(input(...

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

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

那部分应该可以工作。

考虑查看此处的答案以获取有关如何改进菜单的提示:Creating a Menu in Python

【讨论】:

    猜你喜欢
    • 2021-08-06
    • 2018-09-05
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2018-01-20
    • 2017-12-16
    相关资源
    最近更新 更多