CMIS 102 input() A program that can't input data from the user or from a file is very limited because it can do only one computation, the one based on its built-in constant data. To run another computation, the program itself would have to be changed and run again. The example programs looked at so far, with the data built-in, have been unnatural. You want the same program to input different sets of data each time it is run. Another word for input is "read". Doing input is very simple in Python, although there are some details that you need to be aware of so as not to make mistakes. To input from the user (more precisely, the keyboard), use the input() function. This example reads the string that the user types in on the keyboard and assigns it to the x variable: x = input("Please type a word or line and then press the Enter key") print("Thank you for that wonderful stuff:", x) This will input as one string whatever line the user types, up to the Enter, including all leading and trailing spaces. Another example: fname = input("Enter your first name: ") lname = input("Enter your last name: ") print("Welcome",fname,lname) The input() function returns a string, always, even if only digits are entered. So a number typed will still be a string, not an int or a float. x = input("Enter a number: ") y = x / 2 # will always crash! A string (which is what x is) can not do division. The string that input() returns must be converted to a numeric value, with the eval() function: [or the int() or float() function] x = eval(input("Enter a number: ")) y = x / 2 # OK, unless not a number was entered print(y) eval() skips over any blanks and tabs. The user should enter the correct type of data. The only real error is if the user tries to enter letters or punctuation for a number. if user types hello, or 123df, the program will crash. We won't worry about how to deal with this situation, as a real program would have to. It's some messy details that isn't covered in the course because it's not directly relevant to the big picture of learning how to create algorithms and programs. [See the input_int.py in the Bonus section.] You usually want to "prompt" the user to indicate what input is expected; the user won't know what to input otherwise. num_quinces = eval(input("Enter the number of quinces you'd like to buy: ")) print("Thank you for your order of", num_quinces, "quinces.") Instead of using the eval() function, usually either the int() or float() functions are used. If an integer is to be input, int() converts the string returned by input() into an int. If a float is to be input, float() converts the string returned by input() into an float. int_var = int(input("Enter an integer: ")) float_var = float(input("Enter a real number: "))