简体   繁体   English

在 python 3 中将字符串转换为 int 时,Try/except 语句未捕获 ValueError

[英]Try/except statement not catching ValueError when converting string to int in python 3

I am trying to convert get input from the user as an integer only, and provide error checking to make sure the user's input is valid.我正在尝试将来自用户的输入转换为仅 integer,并提供错误检查以确保用户的输入有效。 When checking to see if the user has entered a string, the program is supposed to display an error if what the user enters is not an integer.在检查用户是否输入了字符串时,如果用户输入的不是 integer,程序应该显示错误。 However, no matter how I structure this try/except, it is not displaying the error message, and instead bouncing me back to the Visual Studio editor and throwing the exception "invalid literal for int() with base 10".但是,无论我如何构造这个 try/except,它都不会显示错误消息,而是将我弹回 Visual Studio 编辑器并抛出异常“invalid literal for int() with base 10”。 I have searched and searched all over this site and others, and everybody else with this issue is able to get it working using this code, but for some reason for me it just is not working.我已经在这个站点和其他站点上进行了搜索和搜索,其他所有遇到此问题的人都可以使用此代码使其正常工作,但由于某种原因,它无法正常工作。 Can someone please help me figure out what is going wrong, or if there is another way to check for integer input, please point me in that direction.有人可以帮我弄清楚出了什么问题,或者如果有另一种方法来检查 integer 输入,请指出我的方向。 Any assistance is appreciated, and I apologize if there is a similar question on this site already, but I was unable to find one pertaining to my specific case.感谢您提供任何帮助,如果此站点上已经存在类似问题,我深表歉意,但我无法找到与我的具体案例有关的问题。 I am using the Python 3.7 interpreter (although 3.8 is what I have installed, VS does not yet support it).我正在使用 Python 3.7 解释器(虽然我已经安装了 3.8,但 VS 还不支持它)。 Here is my code:这是我的代码:

    try:
        a = int(input("Input: "))
    except ValueError:
        print(a)

The code that you show is wrong.您显示的代码是错误的。

If an exception occurs, then a will not have been assigned.如果发生异常,则不会分配a The code that was working for others was probably very similar but different.为其他人工作的代码可能非常相似但不同。

Here an example with try except, that prompts the user until there is some valid input.这里有一个 try except 的例子,它会提示用户直到有一些有效的输入。 It also prints the path to the python executable and its version in order to be sure, that this is not a version issue.它还打印 python 可执行文件的路径及其版本,以确保这不是版本问题。 an IDE should not be able to ignore a try except statement. IDE 不应忽略 try except 语句。

I never heard of something like this and assume, that there is some other difference than pycharm vs Visual studio code.我从未听说过这样的事情,并假设除了 pycharm 与 Visual Studio 代码之外还有其他一些区别。

import sys
print(sys.executable, sys.version_info)  # for debugging only
while True:
    inp = input("Input: ")
    try:
        a = int(inp)
        break
    except ValueError:
        print("invalid input", inp, ". You should enter a number. Please retry")

print("A is", a)

I don't know whether you want to force the user to enter correct data or whether you just want to detect an error.我不知道您是要强制用户输入正确的数据还是只想检测错误。 Just adapt it to your exact needs只需根据您的确切需求进行调整

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

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