Ask Question
22 June, 13:07

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

+1
Answers (1)
  1. 22 June, 14:59
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers