CMPT 120 Chapter Notes - Chapter 2: Type Conversion

22 views2 pages
meghan78 and 39786 others unlocked
CMPT 120 Full Course Notes
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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents