Created
June 5, 2013 21:44
-
-
Save skopp/5717562 to your computer and use it in GitHub Desktop.
python raw input in haml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!!! | |
%html{:xmlns => "http://www.w3.org/1999/xhtml"} | |
%head | |
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/ | |
%title raw_input and input 2.6 vs. 3.0 | |
:css | |
%body | |
%p input statements | |
%p | |
using the input() and raw_input() functions | |
%br/ | |
implicitly casting | |
%table{:border => "1", :cellpadding => "1", :cellspacing => "1", :width => "864"} | |
%tr | |
%td{:colspan => "2"} | |
%div{:align => "center"} | |
%br/ | |
Version 2.6 and 2.7 (these are the versions we'll be using) | |
%br/ | |
%br/ | |
%tr | |
%td{:width => "347"} | |
%pre.style1 var = raw_input( "prompt string" ) | |
%td{:width => "504"} returns whatever user user typed - up to hitting the ENTER key - as a string | |
%tr | |
%td | |
%pre.style1 | |
:preserve | |
var = input( "prompt string" ) | |
# SUGGESTION: DON'T %td | |
returns the numeric value the user types | |
%br/ | |
%em actually | |
returns the evaluation as a numeric type of the keystrokes typed | |
%tr | |
%td{:colspan => "2"} | |
%div{:align => "center"} | |
%br/ | |
Version 3.x (this is the "new" version) | |
%br/ | |
%br/ | |
%tr | |
%td | |
%pre.style1 var = input( "prompt string" ) | |
%td returns whatever user user typed - up to hitting the ENTER key - as a string | |
%p The prompt string is provided to, well, prompt the (who is almost always an idiot) to type what the programmer wants from them. | |
%p | |
Whatever the user types is taken up to but not including the ENTER (newline character - | |
= succeed ")" do | |
%span.style1 '\n' | |
and returned. | |
%p | |
The cursor stays on the same line as the prompt string and there is no blank place after it. | |
%br/ | |
So make sure you are happy with your UI (user interface) | |
%p Ugly:(user input shown in bold) | |
%p.style1 | |
\>>> raw_input("Please enter your name") | |
%br/ | |
Please enter your name | |
%b test | |
%br/ | |
'test' | |
%br/ | |
%p Pretty:(user input shown in bold) | |
%p.style1 | |
\>>> raw_input("Please enter your name ") | |
%br/ | |
Please enter your name | |
%b test | |
%br/ | |
'test' | |
%br/ | |
%p.style2 | |
Anything the user types is taken - up to but | |
%em not including | |
ENTER - and returned | |
%p.style1 | |
\>>> raw_input("Go--->>> ") | |
%br/ | |
Go--->>> "this is a test" | |
%br/ | |
'"this is a test"' | |
%p.style2 If the user types nothing before ENTER, the NULL STRING is returned. | |
%p.style1 | |
\>>> raw_input("Go--->>> ") | |
%br/ | |
Go--->>> | |
%br/ | |
'' | |
%p.style2 | |
A dumb programmer could decide not to provide a prompt string, which would be a horrible design for their UI. | |
%br/ | |
(And we don't like dumb programmers.) | |
%p.style1 | |
\>>> raw_input() | |
%br/ | |
%p.style3 | |
[at this point the user stares at the screen, waiting and waiting and waiting for something to happen...] | |
%br/ | |
BAD PROGRAMMER! | |
%p | |
When you need a numeric input, you'll need to explicitly cast by calling the appropriate function that coerces a string to a numeric type. | |
%br/ | |
%br/ | |
%span.style1 | |
\>>> intVal = int(raw_input("enter a small integer value: ")) | |
%br/ | |
\>>> floatVal = float(raw_input("enter a floating point value: ")) | |
%br/ | |
%span.style1 | |
\>>> longVal = long(raw_input("enter a integer value (it can be large): ")) | |
%br/ | |
%p | |
Notice that we used the returned value of the function call to | |
%span.style1 raw_input() | |
as the parameter to the | |
%span.style1 long() | |
cast. | |
%p SNAKEBYTE: | |
%p input vs. raw_input | |
%p | |
Mostly you will use the raw_input function. | |
%br/ | |
For the | |
%span.style1 input | |
function, things are a bit different and going to be even worse when you move to 3.0 | |
%p In python version 2.6/7: | |
%p.style1 | |
\>>> val = input("enter a floating point value: ") | |
%br/ | |
enter a floating point value: 4.3 | |
%br/ | |
\>>> val | |
%br/ | |
4.2999999999999998 | |
%p (Also note the round-off error) | |
%p | |
%span.style1 | |
\>>> val = input("type a valid arithmetic expression: " ) | |
%br/ | |
type a valid arithmetic expression: 34.6 ** 82 | |
%br/ | |
\>>> val | |
%br/ | |
1.6004425935658933e+126 | |
%br/ | |
%p | |
%strong | |
SNAKEBYTE - 2.6/7 vs. 3.x | |
%br/ | |
in Python 3, | |
%span.style1 raw_input | |
has been | |
%em renamed | |
to | |
%span.style1 input | |
and the function previously known as | |
%span.style1 input | |
has been dropped. | |
%p Ouch! | |
%p In this course we'll be using 2.6/7.x so hopefully this won't affect anyone. | |
%p This means that in 3.x you need to do: | |
%p.style1 | |
amountOwed = float( | |
%u> | |
%strong input | |
("How much is the bill? ") # 3.x | |
%p | |
instead of | |
%br/ | |
%p.style1 | |
amountOwed = float( | |
%u> | |
%strong raw_input | |
("How much is the bill? ") # will not compile in 3.x | |
%br/ | |
NameError: name 'raw_input' is not defined | |
%br/ | |
%p | |
To get the 2.6/7 input way of inputting and evaluating expression strings, you'll have call the eval() function to evaluate an "expression string": | |
%br/ | |
%p.style1 | |
valueOfExpression = eval( | |
%u> | |
%strong input | |
("Type a valid arithmetic expression: ") # 3.x | |
%p.style1 | |
%p.style1 | |
%p.style1 | |
%p.style1 | |
%p | |
%p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment