= input("Please enter your name:")
name = name[::-1]
reverse print("Your name in reverse is", reverse)
- 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!
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 typestring
! - Thus, it needs to be saved into a variable:
x = input()
- After the user types “Hi”, for example, it is as if:
x = "Hi"
- After the user types “Hi”, for example, it is as if:
- You can customize an input message by passing a string:
= input("Please enter your name: ") x
Input of numeric values
- Now, suppose we want to calculate the sum of two numbers:
= input("Please enter first number: ")
x = input("Please enter second number: ")
y = x + y
z print("The sum is", z)
What happened???
Converting string to number types
- You can convert a string to a number using the methods
int()
andfloat()
- The string that goes inside the parentheses (which we call the “argument” of the method) will be turned to an integer/float
= input("Please enter your age: ")
xstring = int(xstring)
x print("Your age is ", x)
- Just make things shorter by chaining one method into another!
= int(input("Please enter your age: "))
x print("Your age is ", x)