Review
Print Challenge Bruce asked, “When was the last time you had fish?” Now try these: Bruce asked, “When was the last time you had fish?” He’s leading an FEA group. FEA stands for “Fish Eater’s Anonymous.”
Printing variables You can print the data that is stored within a variable by passing the variable as an argument into the print function name = “Donald” name = “Donald” print (name) print (“Hello, my name is”, name) >> Donald >> Hello, my name is Donald
Changing variables Variables are called variables because the data stored in them can be changed You can change the value/data stored in a variable with a second assigning statement dollars = 19.99 print (“I have”, dollars, “in my account”) dollars = 10000.99 print (“Now, I have”, dollars, “in my account”)
Multiple Assignments You can assign multiple variables on the same line: x, y, z = 1, 2, 3 # the variables will assume the values in order You can also assign the same value to multiple variables x = y = z = 10 # variables x, y, and z will all hold the value of 10
Practice You’re working on a simple inventory management system for a small store. You’d like to store the name and price of two products and print them, so that your inventory page looks something like this: Item: Bread, price: $ 1.99 Item: Eggs, price: $ 3.49
Let’s make our programs more interactive! Input Function Let’s make our programs more interactive!
Reading input from the user So far, we have learned how to: OUTPUT information (print function) STORE information (variables) Now, we will learn how to get INPUT from the user, via the keyboard
Input Function You can make your programs interactive by asking the user to input some sort of information, using the input( ) function Notice that again, this keyword has a set of parentheses for an argument This means we CAN use the word input as a variable name
Input Function input ( ) has the same function as print ( ) , in that, they output whatever argument is passed into the function However, the input function only accepts one argument
Input Function The program will wait for the user to enter a response and continue to run the program when the user hits ENTER (in other words, the input function will cause a pause in the execution of your program) WARNING: You must store the data SOMEWHERE! (in a variable)
Input Function Example: username = input (“What is your username?: ” ) print (“Welcome,”, username, “!”) >> What is your username?: Donald >> Welcome, Donald !
Input function The input ( ) will always “return” a string This means that its output will always be treated as a string, as well as when it is stored in a variable price = input (“Give me a number:”) new_price = price+ 2 print (new_price)
Input function The input ( ) will always “return” a string This means that its output will always be treated as a string, as well as when it is stored in a variable price = input (“Give me a number:”) new_price = price + 2 # you are trying to add a string to an integer value print (new_price) # this will run an error because you added two values that were not the same type
Input function (Solution) price = input (“Give me a number:”) new_price = price + “2” print (new_price) >> Give me a number: 43 >> 432
Let’s try it again This time, using the input function, ask the user for the name and price of two products and print out a similar inventory for your store: Item: bread, Price: $ 15.99 Item: orange juice, Price: $ 34.67
Programming Challenge Write a program that allows the user to enter in their first name, and then their last name. Then, greet the user with a welcome message. First Name: Donald Last Name: Seok Welcome, Donald Seok !
Let’s have a little fun … Name a place: Another place A verb: A type of plant: An object: A color: An adjective: A type of weather: A feeling:
Mad Lib Exercise