raw_input function, and through command-line
arguments.
raw_input functionraw_input function, it will wait until
the user to types some text, and presses the Enter key.
The function returns the user's text, as a string.
user_text = raw_input()
user_text2 = raw_input()
print "You typed:", user_text, 'then you typed', user_text2
In the example above, the user won't get any guidance: he or she
will just see a blinking cursor. So, you should pass a prompt to
the raw_input function:
name = raw_input('What is your first name?')
last_name = raw_input('What is your last name?')
full_name = name + ' ' + last_name
print 'Hello,', full_name, 'it\'s nice to meet you'
You should remember that raw_input returns strings.
If you are expecting another kind of variable, you'll have to
convert the string value:
principal = float(raw_input('Loan principal: '))
pct_interest = float(raw_input('Annual % interest: '))
months = int(raw_input('Loan length (months): '))
rate = pct_interest / 1200
payment = principal * (rate + rate / ((1 + rate)**months - 1))
print 'Monthly payment:', payment
Notice how the principal and interest are floats,
while the length of the loan is an int.
To do that, you will open a command shell window, and run the
program within the python interpreter. For example, if your
program is in a file called loan.py, you would type:
python loan.py
This is called "using the command line", and the text above is
called a "command".
This will work if you've set things up correctly: you should
have navigated within the command shell, to place yourself in
the folder where the program loan.py is. Also,
the PATH variable has to be set up correctly, so
the shell knows where the python program is. Here,
I won't tell you how do that.
Suppose the text file loan.py contains the
program I showed you above:
principal = float(raw_input('Loan principal: '))
pct_interest = float(raw_input('Annual % interest: '))
months = int(raw_input('Loan length (months): '))
rate = pct_interest / 1200
payment = principal * (rate + rate / ((1 + rate)**months - 1))
print 'Monthly payment:', payment
then typing the command python loan.py would
prompt the user for principal, interest, and length, and would
print the payment amount. However, sometimes we want to
include that information in the command itself:
python loan.py 12345.00 8.75 60
Those three numbers 12345.00 8.75 60 are called
"command-line arguments". In order for your program to access
them, it must use the argv list, which is part
of the sys library. Here is how:
import sys
principal = float(sys.argv[1])
pct_interest = float(sys.argv[2])
months = int( sys.argv[3])
rate = pct_interest / 1200
payment = principal * (rate + rate / ((1 + rate)**months - 1))
print 'Monthly payment:', payment
Notice several things:
sys.arg[1], the
second is sys.arg[2], etc.
sys library, using import.
float or int, just
as with raw_input.
python loan.py 12345.00 8.75
the program would crash, complaining
that, in sys.argv[3] the index 3 is
out of range. You could prevent this embarassing error, by
telling the user to include 3 arguments. You can actually
figure out how many were typed, by checking
the length of the list sys.argv, as
follows:
import sys
if len(sys.argv) != 4:
print 'Please enter three command-line arguments:'
print 'python loan.py principal interest loan_length'
sys.exit(1)
principal = float(sys.argv[1])
pct_interest = float(sys.argv[2])
months = int( sys.argv[3])
rate = pct_interest / 1200
payment = principal * (rate + rate / ((1 + rate)**months - 1))
print 'Monthly payment:', payment
Here, if the user hasn't typed three arguments, we print out
some instructions, and stop the program by
calling sys.exit.
Did you notice if len(sys.argv) != 4:? It turns
out that the list sys.argv has an extra
entry, sys.argv[0], which is the name of your
program: in this case sys.argv[0]
is 'loan.py'. If you expect a certain number of
arguments, the length of sys.argv should be one
more than that number.