"- Reading and printing floats: [ReadingandPrintingFloats](../CodeExamples/ReadingandPrintingFloats.ipynb)\n",
"- Reading and formatting floats: [ReadingandFormatFloats](../CodeExamples/ReadingandFormatFloats.ipynb)\n",
"- Reading int and float : (more complicated example) [ReadingIntandFloat](../CodeExamples/ReadingIntandFloat.ipynb)\n",
"Download and run these programs; also try giving the wrong input types\n",
"and see what happens. It will *crash* with a *Traceback* which is\n",
"**not** friendly; try this out and learn to make sense of this, you will\n",
"see this a lot in your programming career, so it helps to make sense now of some of the messages you will see."
">>- Reading and printing floats: [ReadingandPrintingFloats](../CodeExamples/ReadingandPrintingFloats.ipynb)\n",
">>- Reading and formatting floats: [ReadingandFormatFloats](../CodeExamples/ReadingandFormatFloats.ipynb)\n",
">>- Reading int and float : (more complicated example) [ReadingIntandFloat](../CodeExamples/ReadingIntandFloat.ipynb)\n",
">>Download and run these programs; also try giving the wrong input types\n",
">>and see what happens. It will *crash* with a *Traceback* which is\n",
">>**not** friendly; try this out and learn to make sense of this, you will\n",
">>see this a lot in your programming career, so it helps to make sense now of some of the messages you will see."
]
},
{
...
...
%% Cell type:markdown id:033593dd tags:
# Printing and Reading to/from the Terminal
## Print to the terminal
Let’s look at how to print to the terminal using the `print()` function. This
prints a `str`, so for example:
```python
print("Hello world")
f="Fred"
g="Ginger"
print("The dancers were "+f+" and "+g)
```
The first line will print `Hello world` and the fourth will form a string containing `The dancers were Fred and Ginger` and then print it to the terminal, in both cases followed by a newline.
The `print()` function is most useful is when it is combined with the `str()` function, which converts other data types into `str` so allowing them to be easily printed, for example:
```python
radius=63.256
print("The radius is : "+str(radius))
z=16.56+74.4j
print("Complex value is : "+str(z))
```
In both lines two and four the `str()` function forms a string from the
numerical variable which is appended to the `str` in quotes and printed in a
reasonable format.
### Key Point
The simplest route to print the value of any variable is to:
- convert it to a str using the `str()` function,
- then use the `print()` function to print the `str`.
A fancier route is the `str.format(`) function, which is covered in `Formatting` section with many options to format your `str`, but this is not needed for this course.
> Write yourself a test program to output your name and address to the terminal.
> **The `print` without the brackets you will find in many on-line examples is the
old syntax; do not use it, or your code will NOT work!!!
## Reading input from the terminal
To get input from the terminal we use the `input()` function. This reads a `str` from the terminal, so for example:
```python
name=input("Type your name : ")
print(name)
```
This will prompt you at the terminal with `"Type your name : "`. It will read this into to `str` variable `name` and then print it back.
> **Example Code:** Reading and printing out a string: [ReadingandPrintingStrings](../CodeExamples/ReadingandPrintingStrings.ipynb)
Note: the lines that start with `#` are *comments* and do not form part of
the code. Try running the code example to see what it does.
The `input()` function really becomes useful when you combine it with the `float()`, `int()` or `complex()` functions to read numerical values from the terminal, so for example
```python
n=int(input("Number of circles : "))
radius=float(input("Give the radius : "))
```
-**Line one** will prompt for the `Number of circles : ` and read the typed value into variable `n` which will be an `int`.
-**Line two** will prompt for `Give the radius : ` and read the typed value into the variable `radius` which will be a `float`.
> When reading `int` or `float` the use of `int(input(…))` or `float(input(…))` is required to ensure you get the correct variable type.
This is **NEW** in `Python-3`, you will find old `Python-2` code online that just used `input(…)` to read `int` and `float`s, but this will not work in Python-3.
This is your first taste of interactive programming.
> **Old- issue**: There is a subtle `Python-2` to `Python-3` issue here. In `Python-2` the string returned by `input()` is automatically evaluated by the `eval()` function before being returned where in `Python-3` it is returned as typed.
Basic programmers will *not* notice the difference and the implications
of this are beyond the scope of this course.
>> **Example Code :**
- Reading and printing floats: [ReadingandPrintingFloats](../CodeExamples/ReadingandPrintingFloats.ipynb)
- Reading and formatting floats: [ReadingandFormatFloats](../CodeExamples/ReadingandFormatFloats.ipynb)
- Reading int and float : (more complicated example) [ReadingIntandFloat](../CodeExamples/ReadingIntandFloat.ipynb)
Download and run these programs; also try giving the wrong input types
and see what happens. It will *crash* with a *Traceback* which is
**not** friendly; try this out and learn to make sense of this, you will
see this a lot in your programming career, so it helps to make sense now of some of the messages you will see.
>>- Reading and printing floats: [ReadingandPrintingFloats](../CodeExamples/ReadingandPrintingFloats.ipynb)
>>- Reading and formatting floats: [ReadingandFormatFloats](../CodeExamples/ReadingandFormatFloats.ipynb)
>>- Reading int and float : (more complicated example) [ReadingIntandFloat](../CodeExamples/ReadingIntandFloat.ipynb)
>>Download and run these programs; also try giving the wrong input types
>>and see what happens. It will *crash* with a *Traceback* which is
>>**not** friendly; try this out and learn to make sense of this, you will
>>see this a lot in your programming career, so it helps to make sense now of some of the messages you will see.