Input is described as anything we send to the program whether it be by keyboard,
mouse, webcam, microphone, etc. We will be using the keyboard and collecting
information from users, making our programs interactive.
print "What is your name?" name = raw_input() print "Hi,", name
What is your name? foobar Hi, foobar
Basically we used a built-in function ‘raw_input()’ to collect information from the user
and assign it to a variable. Once in the variable we can spit it back out at the user. We
can also manipulate it as the next example will show. Alternatively, we could have
used name = raw_input(“What is your name?”), instead of using print, with the only
difference being that the user input would be on the same line as our “What is your
name?” string. Now for numbers we use a little different function.
print "Addition" a = input("Please select a number:") b = input("And another one:") c = a + b print a, "plus", b, "equals", c
Using the input() function we can assign the user input as numbers. If you try to assign a letter or character other than a number our program will shut down and give you an error. Also, assigning numbers with raw_input() will just put the numbers into a string (i.e. “63”) meaning you can’t manipulate them.
- Make a program that gets the user’s name, favorite color, and a number. Output
hello, “their name”! and then their favorite color times the number they entered.