Quiz #4 for October 13, 2020

Please use your Python system (IDLE or something else) to compute the following. Be prepared to show your work!

  1. Write a program that reads two integers, and then prints a message saying whether the first is less than, equal to, or greater than the second.

    Answer:

    
    try:
        # read in the numbers
        val1 = int(input("First number> "))
        val2 = int(input("Second number> "))
    except:
        # oops – something went wrong
        print("Both numbers must be integers")
    else:
        # got the numbers; now compare
        if val1 < val2:
            print(val1, "is less than", val2)
        elif val1 > val2:
            print(val1, "is greater than", val2)
        else:
            print(val1, "and", val2, "are equal")
    


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
MHI 289I, Programming for Health Informatics
Version of October 14, 2020 at 11:50PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh