Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的ValueError异常处理_Python_Python 3.x_Exception - Fatal编程技术网

Python中的ValueError异常处理

Python中的ValueError异常处理,python,python-3.x,exception,Python,Python 3.x,Exception,我想在Python中处理ValueError。但是,每当我键入下面的代码时,我都会得到一个错误,我希望得到我所期望的输出 a, b = map(int, input().split()) try: print(a//b) except ZeroDivisionError as e: print('Enter code: ', e) except ValueError as e: print('Enter code: ', e) 如果a和b的输入为“1”和“$”,则 Va

我想在Python中处理ValueError。但是,每当我键入下面的代码时,我都会得到一个错误,我希望得到我所期望的输出

a, b = map(int, input().split())
try:
    print(a//b)
except ZeroDivisionError as e:
    print('Enter code: ', e)
except ValueError as e:
    print('Enter code: ', e)
如果a和b的输入为“1”和“$”,则
ValueError的预期输出:“输入代码:以10为基数的int()的文本无效:$

您的问题出现在您的
尝试之前,此处
a,b=map(int,input().split())

$
被赋予了
int
,这将失败并引发
无效的int()文本,基数为10:$
,这是非常明确的

try:
    a, b = map(int, input().split())
    print(a//b)
except ZeroDivisionError as e:
    print('Enter code: ', e)
except ValueError as e:
    print('Enter code: ', e)

谢谢,真的很有帮助me@ShivManoharlalSharma考虑阅读异常,它们很有帮助;)@Shivmanoharalsharma你现在可以思考你的问题是什么?这回答了你的问题吗?