Friday, 15 September 2017

Inputting Information

One major caveat for this post is that it uses Python 3.x (Python 3.6 to be precise). Input works differently in Python 2.x, particularly with regards to data types. If you are using Python 2.7 or earlier, the scripts here may not work for you. 

So far our scripts have just been run using the data already stored in them. Variables are initialised within the code, and any output is calculated  or derived from that. What if a user wants to change something without necessarily editing the code? This is where input() comes in.
Here's a very short script to demonstrate.
#!/usr/bin/python3
name = input("Hello, what is your name? ")
print ("Greetings, ", name)
When I run it, I get the output:
  RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03e_simpleinput.py
Hello, what is your name?
It is prompting me for input, and I don't want to disappoint. So I type in my name and hit enter (pressing enter tells Python I have finished inputting - it enters the information I have typed into the program).

End result is
 RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03e_simpleinput.py
Hello, what is your name? John
Greetings,  John
>>>
Just for clarification, the string inside the brackets of the input() statement is the question or prompt that appears onscreen - this tells the user of the program what is expected of them.
Another important point to bear in mind is that the same line of code is also a variable assignment. Whatever is inputted by the user is assigned to the variable name (or whatever variable the programmer wants). If the information were not stored in a variable or otherwise used, it would be lost as soon as it was entered. As it is, whatever is stored in name is used in the final print statement.

So can we do the same with numbers? Lets have a go.
#!/usr/bin/python3
print ("Squaring program!")
number = input("Please enter number to be squared: ")
print (number ** 2)

What we get is an error message.
 RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03_numberinput.py
Squaring program!
Please enter number to be squared:
6
Traceback (most recent call last):
  File "C:\Users\John\Dropbox\Misc Programming\Python\python3\test03_numberinput.py", line 5, in <module>
    print (number ** 2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

>>>
More specifically we get a type error - it was expecting a float or an integer, but got a string instead, and it's pointing out that it can't work out the square of a string. This is an important point when using input to enter data - any information inputted on the command line is treated as a string, i.e. a piece of text.
There is  a simple solution. Remember in the post about maths, I mentioned that float() tries to convert a string to a floating point number, and int() tries to convert a string to an integer?
Here is a modified version of the script.
#!/usr/bin/python3
print ("Squaring program!")
number = input("Please enter number to be squared: ")
number = float(number) # converts from string to float
print (number ** 2)

And here is how it looks when I run  it.
 RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03_numberinput.py
Squaring program!
Please enter number to be squared:
13
169.0
>>>
And hey presto, it works as intended!
The type() function is useful in working out what is going on with such errors, particularly when combined with print(). It returns the data type of whatever is within the function's brackets. This can be shown by adding a few type() statements into this script to show what data type is held in the number variable.
#!/usr/bin/python3
print ("Squaring program!")
number = input("Please enter number to be squared: ")
print (type(number))
number = float(number)
print (type(number))
print (number ** 2)

And the output is:
 RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03_numberinput.py
Squaring program!
Please enter number to be squared:
17
<class 'str'>
<class 'float'>
289.0

>>>
 thus showing the change from an inputted string ('str') to a floating point ('float') which can then be used in calculations.


No comments:

Post a Comment