简体   繁体   English

ValueError:以10为底的int()的无效文字:''

[英]ValueError: invalid literal for int() with base 10: ''

I'm new in Python 3 and need some help, my error is: 我是Python 3的新手,需要帮助,我的错误是:

"ValueError: invalid literal for int() with base 10: ''  " 

any ideas? 有任何想法吗? The code I'm using is the following: 我正在使用的代码如下:

liste = [1, 2, 3]

def liste_pop():
    print(liste)
    pop = int(input('remove = Enter um das letzte Element der Liste auszugeben + entfernen oder die Position eingeben'))
    liste.pop(pop)
    return

You did not enter a number during input, instead you simply pressed enter (and got back the empty string). 在输入过程中,您没有输入数字,而是只需按Enter(然后返回空字符串)。 You need to add code to handle the situation where a user enters invalid input , in Python, this is performed with a try-except where you specify the error you're expecting ValueError in the except clause: 您需要添加代码来处理用户输入无效输入的情况 ,在Python中,这是通过try-except执行的,其中您在except子句中指定了期望ValueError的错误:

def liste_pop():
    print(liste)
    while True:
        try:
            pop = int(input('remove = Enter um das letzte Element der Liste auszugeben + entfernen oder die Position eingeben'))
            break
        except ValueError as e:
            print("Only numbers accepted")
    liste.pop(pop)

Of course, this has an additional issue, what if a user enters a number outside the accepted range? 当然,这还有一个额外的问题,如果用户输入的数字超出了可接受范围,该怎么办? An IndexError is raised; 引发IndexError you'll need another try-except for that (I'll let you handle it on your own.) try-except ,您还需要其他try-except (我会让您自己处理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM