Ask Question

Three variables, x, y and z, supposedly hold strings of digits, suitable for converting to integers. Write code that converts these to integers and print the sum of these three integers. However, if any variable has a value that cannot be converted to an integer, print out, the string "bad value (s) in: " followed by the names of the variables that have bad values (separated by spaces, in alphabetically ascending order). For example, if the values of x, y and z were respectively "3", "9", "2" then the number 14 would be printed; but if the values were "abc", "15", "boo" then the output would be: bad value (s) in: x z

+3
Answers (1)
  1. 17 February, 16:23
    0
    Required code is given below:

    Explanation:

    x, y, z = "abc", "15", "boo"

    errors = []

    try:

    x = int (x)

    except ValueError:

    errors. append ('x')

    try:

    y = int (y)

    except ValueError:

    errors. append ('y')

    try:

    z = int (z)

    except ValueError:

    errors. append ('z')

    if len (errors) = = 0:

    print (x+y+z)

    else:

    print ('bad value (s) in: ' + ' '. join (errors))
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Three variables, x, y and z, supposedly hold strings of digits, suitable for converting to integers. Write code that converts these to ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers