Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The input() function


For simple applications, the input() function can be used to gather input from a user. This function writes a prompt and accepts input. The returned value is a string. We might use this in a script file as follows:

c= float(input("Temperature, C: "))
print("f =", 32+9*c/5)

This will write a simple prompt on the console, and accept a string as input. The string value will be converted to a floating-point number, if possible. If the string is not a valid number, the float() function will raise an exception. This will then print a line of output.

Here's how it looks when we run it:

MacBookPro-SLott:Code slott$ python3 Chapter_4/ex_1.py
Temperature, C: 11
f = 51.8

We've highlighted the command, which is entered after the OS shell prompt. The statements in the script file, named as part of the command, are executed in order.

Our input to Python, 11, is also highlighted, to show how the input() function supports simple interaction.

The input() function only returns a Unicode string...