Input/Output

  • Programming is nothing without the design of an interface!
    • I have to be able to input data in the program, and
    • I have to be able to get results (output) from the program.

Graphical input/output

  • Also called Graphical User Interface (GUI) - we’ll study it in Unit 10
  • Kind of mimics the way we use mechanical input and output
  • Traditionally, WIMP (Windows, Icons, Menus and Pointers)

Text input/output

  • Even simpler, however, it is a good start for programming!
name = input("Please enter your name:")
reverse = name[::-1]
print("Your name in reverse is", reverse)

The command-line interface will ask for input from our keyboard, and then:

Python text output: print()

  • Put what you want to print between the parentheses: print("Hello World")
  • If you want to jump to a new line, use \n: print("Hello\nWorld")
  • You can also pass multiple arguments by separating them with commas: print("x has the value:", x, "\nand y has the value:", y)

Python text input: input()

  • The command waits until the user types some text in the command-line interface and finishes with ENTER
  • The term input() “turns” into the text entered, and is ALWAYS an object the type string!
  • Thus, it needs to be saved into a variable: x = input()
    • After the user types “Hi”, for example, it is as if: x = "Hi"
  • You can customize an input message by passing a string:
x = input("Please enter your name: ")

Input of numeric values

  • Now, suppose we want to calculate the sum of two numbers:
x = input("Please enter first number: ")
y = input("Please enter second number: ")
z = x + y
print("The sum is", z)

What happened???

Converting string to number types

  • You can convert a string to a number using the methods int() and float()
    • The string that goes inside the parentheses (which we call the “argument” of the method) will be turned to an integer/float
xstring = input("Please enter your age: ")
x = int(xstring)
print("Your age is ", x)
  • Just make things shorter by chaining one method into another!
x = int(input("Please enter your age: "))
print("Your age is ", x)