listing19-2.py 634 B

123456789101112131415161718192021
  1. from configparser import ConfigParser
  2. CONFIGFILE = "area.ini"
  3. config = ConfigParser()
  4. # Read the configuration file:
  5. config.read(CONFIGFILE)
  6. # Print out an initial greeting;
  7. # 'messages' is the section to look in:
  8. print(config['messages'].get('greeting'))
  9. # Read in the radius, using a question from the config file:
  10. radius = float(input(config['messages'].get('question') + ' '))
  11. # Print a result message from the config file;
  12. # end with a space to stay on same line:
  13. print(config['messages'].get('result_message'), end=' ')
  14. # getfloat() converts the config value to a float:
  15. print(config['numbers'].getfloat('pi') * radius**2)