#!/usr/bin/env python # coding: utf-8 # In[9]: # Section 4.5 - sententials # Definition: A sentential is a special symbol that # marks the end of a sequence of values. # # Situation: where you have some kind of while loop but # you don't know how many times the loop should go. # # Options: # 1. - Ask user if they have more information to give to # the computer. (downside: could be cumbersome) # 2. - Ask at the beginning how many times the loop should # go. (downside: can be painful for the user) # 3. - use a sentential! # # Example program -- imagine a doctor who has to insert the # weights of all patients seen that week, every week. And # report the average weight. # Going to use a sentential to "end" the loop. # # Write our prompt for the weight like this: # "Enter weight or 0 if no more weights." # # Recall: the average of n items is (x1+x2+...+xn)/n count=0 total_weight=0.0 new_weight=float(input("Insert a weight or insert 0 to stop the program.")) while new_weight != 0: total_weight+=new_weight count+=1 new_weight=float(input("Insert a weight or insert 0 to stop the program.")) if count!=0: print("The average weight of the",count,"entries is",total_weight/count) # In[22]: # Format function -- how to get two decimal points of accuracy # var=1489234237498498232.12753575423456877 # the following code will give two decimal points # it ROUNDS print(format(var,',.2f')) # the following gives scientific notation with ("two sig figs"?) print(format(var,',.2e'))