Ask Question
19 December, 15:12

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

+5
Answers (1)
  1. 19 December, 17:31
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers