What are variables? Using input() 142-253 Computer Programming BSc in Digital Media, PSUIC Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 2. Variables What are variables? Using input()
1. What is a Variable? A variable is a "named tag" for data used in your programs "Aj. Andrew" tag data Python treats the tag name teacher as being a different name than Teacher "Teacher" is a string, not the tag name Teacher
Variables Using Number Data
Why are they called "Variables"? a variable can change (the tag is given new data 5 9 first
Many Tags for One Piece of Data Teacher "Aj. Andrew" GreatTeacher
Moving a Tag to New Data changing one variable (tag) does not affect the other variable Great Teacher "Einstein "Aj. Andrew" Teacher
Numbers and Strings + means different things for numbers and strings strings can use "…" or '…'
The New Me the "new" score is the "old" score + 2
2. Have a Program get Input from the User Write the program "SayHello.py" in any text ediror (e.g. Notepad, Notepad++)
Load the Program into IDLE
Run the program in IDLE
the program user types this name the program uses input() to assign the user's data to yourName, and then prints it
Run the Program Many Times the user types in a different name each time
Run the Program in the Command Window Find the program and then run it twice.
input() reads in a string The data for variable x is the string "1234" This data is NOT the number 1234 use int() to change the data from a string to a number
Getting a number as input The data for variable x is an integer The data for variable x is a float (a decimal) If Python wants an integer input, it will be upset if the user enters a float
3. Farenheit to Celsius Converter tempConverter.py means no newline str() changes a number to a string comments to help a reader
Execution the user typed this
Errors in a Program
Where's "line 10"? string() should be str()