Python CodeLab关于异常的问题--尝试,ValueError除外

Python CodeLab关于异常的问题--尝试,ValueError除外,python,python-3.x,python-2.7,exception,valueerror,Python,Python 3.x,Python 2.7,Exception,Valueerror,问题: error_output = "Bad values in:" numeric_output = 0 inputs = input().strip().split(" ") try: x = int(inputs[0]) numeric_output += x except ValueError: error_output += " x" try: y = int(inputs[1]) numeric_output += y except Valu

问题

error_output = "Bad values in:"
numeric_output = 0
inputs = input().strip().split(" ")
try:
    x = int(inputs[0])
    numeric_output += x
except ValueError:
    error_output += " x"
try:
    y = int(inputs[1])
    numeric_output += y
except ValueError:
    error_output += " y"
try:
    z = int(inputs[2])
    numeric_output += z
except ValueError:
    error_output += " z"
if error_output != "Bad values in:":
    print(error_output)
else:
    print(numeric_output)
try:
    print(int(x) + int(y) + int(z))
except ValueError:
    print("bad value(s) in:")
    if not x.isdigit():
        print(' x')
    if not y.isdigit():
        print(' y')
    if not z.isdigit():
        print(' z')
“三个变量——x、y和z——假定包含适合转换为整数的数字字符串。编写将这些变量转换为整数的代码,并打印这三个整数的总和。但是,如果任何变量的值无法转换为整数,请打印出字符串”中的坏值:后跟具有错误值的变量的名称(以空格分隔,按字母升序排列)

例如,如果x、y和z的值分别为“3”、“9”和“2”,则将打印数字14。另一方面,如果值是“abc”、“15”和“boo”,那么输出将是:x z中的:坏值。”


注意:这个问题来自图灵的Craft CodeLab,它似乎需要一个特定的标准输入才能得到正确的答案。我在每次尝试后都加入了CodeLab提供给我的“错误分析”,让你了解它所寻找的格式


尝试#1

error_output = "Bad values in:"
numeric_output = 0
inputs = input().strip().split(" ")
try:
    x = int(inputs[0])
    numeric_output += x
except ValueError:
    error_output += " x"
try:
    y = int(inputs[1])
    numeric_output += y
except ValueError:
    error_output += " y"
try:
    z = int(inputs[2])
    numeric_output += z
except ValueError:
    error_output += " z"
if error_output != "Bad values in:":
    print(error_output)
else:
    print(numeric_output)
try:
    print(int(x) + int(y) + int(z))
except ValueError:
    print("bad value(s) in:")
    if not x.isdigit():
        print(' x')
    if not y.isdigit():
        print(' y')
    if not z.isdigit():
        print(' z')
虽然这段代码在Idle和Pycharm上运行都没有问题,但在CodeLab上它不是正确的答案……下面是我第一次尝试的CodeLab的错误分析:

error_output = "Bad values in:"
numeric_output = 0
inputs = input().strip().split(" ")
try:
    x = int(inputs[0])
    numeric_output += x
except ValueError:
    error_output += " x"
try:
    y = int(inputs[1])
    numeric_output += y
except ValueError:
    error_output += " y"
try:
    z = int(inputs[2])
    numeric_output += z
except ValueError:
    error_output += " z"
if error_output != "Bad values in:":
    print(error_output)
else:
    print(numeric_output)
try:
    print(int(x) + int(y) + int(z))
except ValueError:
    print("bad value(s) in:")
    if not x.isdigit():
        print(' x')
    if not y.isdigit():
        print(' y')
    if not z.isdigit():
        print(' z')

  • 在每个测试用例中:a)标准输出不正确和b)没有任何内容打印到标准输出
  • 您几乎可以肯定应该使用:+
  • 我还没有看到正确的解决方案使用:
  • 我还没有看到正确的解决方案使用:“x”
  • 我还没有看到正确的解决方案使用:“y”
  • 我还没有看到正确的解决方案使用:“z”
  • 我们认为您可能需要考虑使用:<强>=< /强> 。 我们认为您可能需要考虑使用:<强>求和>强> 。
  • 采用您的方法的解决方案通常不会使用:!=
  • 采用您的方法的解决方案通常不会使用:(句号)
  • 采用您的方法的解决方案通常不会使用:1
  • 采用您的方法的解决方案通常不会使用:2
  • 采用您的方法的解决方案通常不会使用:[]
  • 使用您的方法的解决方案通常不使用:输入
  • 使用您的方法的解决方案通常不使用:剥离
  • 我们认为您可能需要考虑使用:<强>不<强> 。
    我的第二次尝试:

    error_output = "Bad values in:"
    numeric_output = 0
    inputs = input().strip().split(" ")
    try:
        x = int(inputs[0])
        numeric_output += x
    except ValueError:
        error_output += " x"
    try:
        y = int(inputs[1])
        numeric_output += y
    except ValueError:
        error_output += " y"
    try:
        z = int(inputs[2])
        numeric_output += z
    except ValueError:
        error_output += " z"
    if error_output != "Bad values in:":
        print(error_output)
    else:
        print(numeric_output)
    
    try:
        print(int(x) + int(y) + int(z))
    except ValueError:
        print("bad value(s) in:")
        if not x.isdigit():
            print(' x')
        if not y.isdigit():
            print(' y')
        if not z.isdigit():
            print(' z')
    
    尝试的“错误分析”#2:


  • 在某些测试用例中标准输出不正确
  • 您几乎可以肯定应该使用:=
  • 我还没有看到正确的解决方案使用:“中的错误值:”
  • 我还没有看到正确的解决方案使用:'x'
  • 我还没有看到正确的解决方案使用:'y'
  • 我还没有看到正确的解决方案使用:'z'
  • 您几乎可以肯定应该使用:str
  • 我们认为您可能需要考虑使用:<强>,(逗号)< /强> 。
  • 使用您的方法的解决方案通常不使用:ValueError

  • 最后,这里是我的第三次尝试(根据CodeLab,这是最接近准确的):


    尝试的“错误分析”#3:


  • 在某些测试用例中标准输出不正确
  • 我还没有看到正确的解决方案使用:“中的错误值:”
  • 采用您的方法的解决方案通常不会使用:
  • 使用您的方法的解决方案通常不使用:ValueError


  • 我在这里遗漏了什么?

    为什么不尝试在每个变量之前添加空格呢?我想这会有所帮助。我也面临着这类问题

    try:
        print(int(x) + int(y) + int(z))
    except ValueError:
        print("bad value(s) in:",end='')
        if not x.isdigit():
            print(' x',end='')
        if not y.isdigit():
            print(' y',end='')
        if not z.isdigit():
            print(' z',end='')
    

    这也应该起作用,也许:

    try:
        print(int(x)+int(y)+int(z))
    except ValueError:
        print('bad value(s) in:', end=' ')
        if not x.isdigit():
            print('x', end=' ')
        if not y.isdigit():
            print('y', end=' ')
        if not z.isdigit():
            print('z', end=' ')
    

    谢谢大家的帮助!我发现答案是:

    try:
        sum = int(x) + int(y) + int(z)
        print(sum)
    except ValueError:
        print("bad value(s) in:",end='')
        if not x.isdigit():
            print(' x',end='')
        if not y.isdigit():
            print(' y',end='')
        if not z.isdigit():
            print(' z',)
    

    请更新您的代码,使其成为