Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Three variables , x, y and z, supposedly hold strings of digits, suitable for co

ID: 3698213 • Letter: T

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

In python please.

Explanation / Answer


#Pyhton program
#conversion.py
#The python program that reads three values from console
#and try to convert to integers .If no error then print
#sum to console otherwise print a message where the error
#occures.
def main():
  
        error='bad value(s) in'
        valid=0
        #read x, y and z
        x=raw_input('enter x vlaue : ')
        y=raw_input('enter y vlaue : ')
        z=raw_input('enter z vlaue : ')

        #convert from string to integer
        try:
            x=int(x)
            #set valid to 1 if conversion is success
            valid=1
        except ValueError:
            error+=' x'
            #set valid to 0 if conversion is failed
            valid=0

        #convert from string to integer
        try:
            y=int(y)
            valid=1
        except ValueError:
            error+=' y'
            #set valid to 0 if conversion is failed
            valid=0

        #convert from string to integer
        try:
            z=int(z)
            valid=1
        except ValueError:
            error+=' z'
        #set valid to 0 if conversion is failed
            valid=0

        #check if vlaid is 1
        if valid==1:
            #no error, sum of x ,y and z
            print (x+y+z)
        else:
            print error
      
main()

------------------------------------------------------------------------------------------------------------

Sample python console output:

>>>
enter x vlaue : 5
enter y vlaue : 5
enter z vlaue : 5
15
>>> ================================ RESTART ================================
>>>
enter x vlaue : abc
enter y vlaue : 15
enter z vlaue : boo
bad value(s) in x z

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote