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.
When I run it, I get the output:#!/usr/bin/python3name = input("Hello, what is your name? ")
print ("Greetings, ", name)
RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03e_simpleinput.pyIt 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).
Hello, what is your name?
End result is
RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03e_simpleinput.pyJust 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.
Hello, what is your name? John
Greetings, John
>>>
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.
What we get is an error message.#!/usr/bin/python3print ("Squaring program!")
number = input("Please enter number to be squared: ")
print (number ** 2)
RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03_numberinput.pyMore 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.
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'
>>>
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.
And here is how it looks when I run it.#!/usr/bin/python3print ("Squaring program!")
number = input("Please enter number to be squared: ")
number = float(number) # converts from string to float
print (number ** 2)
RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test03_numberinput.pyAnd hey presto, it works as intended!
Squaring program!
Please enter number to be squared: 13
169.0
>>>
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/python3print ("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.pythus showing the change from an inputted string ('str') to a floating point ('float') which can then be used in calculations.
Squaring program!
Please enter number to be squared: 17
<class 'str'>
<class 'float'>
289.0
>>>
No comments:
Post a Comment