CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: User I/O (Input/Output)


  1. Basic Console Output
  2. Basic Console Input


  1. Basic Console Output
    • Basic print function
      print("Carpe") print("diem")

    • Print on same line
      print("Carpe ", end="") print("diem")

    • Print multiple items
      print("Carpe", "diem") # Another Example: print() # blank line # Compute the hypotenuse of a right triangle a = 3 b = 4 c = ((a**2) + (b**2))**0.5 print("side a =", a) print("side b =", b) print("hypotenuse c =", c)

  2. Basic Console Input
    • Input a string
      name = input("Enter your name: ") print("Your name is:", name)

    • Input a number (error!)
      x = input("Enter a number: ") print("One half of", x, "=", x/2) # Error!

    • Input a number with int()
      x = int(input("Enter a number: ")) print("One half of", x, "=", x/2)