The input() and raw_input() Function
The input() function waits for the user to type
some numbers or a number on the keyboard and press ENTER.
③ myName = input()
This function
call evaluates to a number equal to the user’s input, and the previous line of
code assigns the myName variable to
this number value.
You can think
of the input() function call
as an expression that evaluates to whatever string the user typed in. If the
user entered 4, then the
expression would evaluate to myName = 4. The following call to print() actually contains the expression 'It is good to
meet you, ' + myName between the
parentheses.
④ print('It is good to meet you, ' + myName)
Remember that
expressions can always evaluate to a single value. If 4 is the value stored in myName on the previous line, then this
expression evaluates to 'It is good to meet you,4'. This single number value is then passed to print(), which prints it on the screen.
Same as the raw_input () function but in this function the
it evaluate both numbers and text that is typed by the user. It does the same
thing that the input functions does but the different is that it input()
function accept only numbers or integer and float WHILE raw_input() function
accept both numbers and string or text.
Examples:
Now I feel it is time for a really complicated program. Here it is:
print "Halt!"
user_reply = raw_input("Who goes there? ")
print "You may pass,", user_reply
When I ran it, here is what my screen showed:
Halt!
Who goes there? Josh
You
may pass, Josh
More examples
--------------------------------------------------
a = 123.4
b3 = ‚Spam‚
first_name = "Bill"
b = 432
c = a + b
print "a
+ b is",c
print "first_name
is",first_name
print
"Sorted Parts,
After Midnight or",b3
--------------------------------------------------------------
# This program calculates rate and distance problems
print "Input
a rate and a distance"
rate = input("Rate: ")
distance = input("Distance: ")
print
"Time:",
(distance / rate)
------------------------------------------------------------------
# This program calculates the perimeter and area of a
rectangle
print "Calculate
information about a rectangle"
length
= input("Length: ")
width = input("Width: ")
print "Area",
length * width
print
"Perimeter",
2 * length + 2 * width
--------------------------------------------------------------
string1 = raw_input(“String 1: “)
string2 = raw_input(“String 2: “)
int1 = input(“Integer 1: “)
int2 = input(“Integer 2: “)
print string1
+ string2
print
int1 * int2
--------------------------------------------------------
print "this
is an exercise"
n1 = input("please input the first number: ")
n2 =
input("Please input the second number: ")
print
‘this is number’,n1
print”this
is number”,n2
---------------------------------------------------------------
stg1 = raw_input("Please input a word: ")
stg2
= raw_input("please input a word: ")
print
stg1,stg2
print
stg1
print stg2
Now
test this examples on your android device and on your pc.
Happy
coding season with andriod…….
nice one commenting from tellkiss.com
ReplyDeletethanks man.
Deletei love u.