I'm just playing with input and variables. I'm trying to run a simple function:
slope = (y2-y1)/(x2-x1)
I'd like to prompt the user to enter y2
, y1
, x2
and x1
. What is the simplest, cleanest way to do this?
I'm just playing with input and variables. I'm trying to run a simple function:
slope = (y2-y1)/(x2-x1)
I'd like to prompt the user to enter y2
, y1
, x2
and x1
. What is the simplest, cleanest way to do this?
If the user is entering the inputs in just one line with space as delimiting word between those inputs, you can write:
Now, you can change it to:
The awesome trick is that in this way you don't waste your space creating a new list and storing your values in that and then fetching it.
You can use the
input()
function to prompt the user for input, andfloat
to convert the user input from a string to a float:If you're using python 2, use
raw_input()
instead.You can use:
Where the string 'Please enter a value:' would be your message, and foo would be your variables.
This is the simplest way:
Note that the
raw_input()
function returns a string, which is converted to a floating point number withfloat()
. If you type something other than a number, you will get an exception:If you're using Python 3 (it sounds like you are), use
input
instead ofraw_input
.