CMPT 120 Chapter Notes - Chapter 2: Type Conversion
meghan78 and 39786 others unlocked
29
CMPT 120 Full Course Notes
Verified Note
29 documents
Document Summary
Whenever you use the raw_input function, it will return a string. That"s because as far as the interpreter is concerned, the user just typed a bunch of characters and that"s exactly what a string is. If you want to treat the user"s input as an integer or floating point number, you have to use one of the type conversion functions described above. For example, if you ask the user for their height, you really want a floating point value, but we get a string. In this example, the user enters the string "1. 8", which is returned by the raw_input function. That is converted to the floating point number 1. 8 by the float function. This is stored in the variable m (for metres ). Once there is a floating point value in m, your program can do numeric calculations with it. The number of inches is calculated and the corresponding floating point number is stored in inches.