''' Purpose: relate a cautionary tale -- make sure your expressions do what they are supposed to do ''' # get Celsius temperature c reply = input( 'Enter Celsius temperature: ' ) # ALWAYS hands back a STRING!!! c = int( reply ) # convert our string number into an int print( ) # formula for f = 9/5 c + 32 # python uses PMDAS (power, multiplication-division, addition-subtraction); left to right # compute respectively, f1 and f2, the decimal and integer Fahrenheit equivalents of c # remember that a single / is decimal division --> gives you decimal answer f1 = 9/5 * c + 32 f1 = round( f1, 1 ) # we can simplify the number of decimal places to 1 decimal # the second argument is the number of decimal places you want to round to - this is optional # if not given, it'll just round to the nearest integer # translating algebra to programming can be different # cannot have implicit multiplication, MUST USE * to indicate multiplication # 3c --> not ok # 3 * c --> ok # want integer answer --> we can do integer division with // # but why does 9//5 give you the wrong answer? # 9//5 --> 1 because it drops the decimal # --> 1 * c + 32 = 1 * (24) + 32 = 56 which is not the same as 75.2 F # must be aware about operators and their orders within the formula so that we get the same answer f2 = 9//5 * c + 32 # when you drop the decimals, calculations might be a lot different when involving multiplication or division # f2 = 9 * c // 5 + 32 # make the numerator of division as large as possible before you do division --> gives you a more accurate answer # the problem doesn't tell us how to round, so either int() or round() would be fine in this case # can also use int() f2 = int( f1 ) # we have the decimal version in f1, then to just find the integer version, we use int() to drop the decimal places # use round() f2 = round( f1 ) # round to the nearest integer from the answer in f1 # we didn't give the round() function a second argument, so it rounds to the nearest integer --> so f2 is an int # display results print( c, 'C =', f1, 'F' ) print( ) print( c, 'C =', f2, 'F' )