[Tutor] Function problem

Eri Mendz jerimed at myrealbox.com
Sun Nov 7 13:09:37 CET 2004


Hello Kent & everyone,

Thanks again for helping. For the regexp question i used the re module 
to get it over with. I think the program is fine now, i have modularized 
it into different functions:

#!/usr/bin/env python
# filename: tempConvert.py
# a modular version broken down into separate functions
# Author: Eri Mendz
# Sun Nov  7 13:47:50 AST 2004

import sys, re

def print_options():
     print '''
     THIS IS A TEMPERATURE CONVERSION PROGRAM.
     Options:
     [C] - convert to Celsius
     [F] - convert to Fahrenheit
     [P] - print options
     [Q] - quit
     '''
print_options()

def f2c(ctemp):
     return (9/5.0)*ctemp + 32

def c2f(ftemp):
     return (ftemp - 32) * 5/9.0

def proceed():
     proceed = raw_input("do another conversion? [Y|N]: ")
     yes = re.compile('(y|ye|yep|yes|ok|okey|okay)$', re.IGNORECASE)
     if yes.match(proceed):
       print_options()
     else:
       confirmQuit()

def confirmQuit():
     ask = raw_input("Really quit program? [Y|N]: ")
     yes = re.compile('(y|ye|yep|yes|ok|okey|okay)$', re.IGNORECASE)
     if yes.match(ask):
       sys.exit(bye())         # call function w/n function
     else:
       print_options()

def askFahrenheit():
     try:
         getftemp = float(raw_input("enter fahrenheit temperature: "))
         print "temp in C is: ", c2f(getftemp)
         proceed()
     except ValueError:
         print "error: not a valid input!"
         print_options()

def askCentigrade():
     try:
         getctemp = float(raw_input("enter centigrade temperature: "))
         print "temp in F is: ", f2c(getctemp)
         proceed()
     except ValueError:
         print "error: not a valid input!"
         print_options()

def showErrorPrompt():
         print "invalid option"
         print_options()

def bye():
     print "THANK YOU: GOODBYE!!!"

# begin main menu
while 1:
     choice = raw_input("select options: ")
     if choice == 'c' or choice == 'C':
         askFahrenheit()
     elif choice == 'f' or choice == 'F':
         askCentigrade()
     elif choice =='p' or choice == 'P':
         print_options()
     elif choice =='q' or choice == 'Q':
         confirmQuit()
     else:
         showErrorPrompt()

bye()

I think i can move on now to the next chapter of my tutorial. I'm 
intrigue of Bob's class version but i think that can wait until i reach 
the topic classes.

-- 
Regards,
erimendz ** use firefox/thunderbird **
http://www.spreadfirefox.com/?q=affiliates&id=6670&t=58


Kent Johnson wrote:
 > At 12:26 PM 11/6/2004 +0300, Eri Mendz wrote:
 >
 >>               getftemp = int(raw_input("enter fahrenheit 
temperature: "))
 >>
 >> How do i make the program accept decimal numbers, not just integers?
 >
 >
 > Use float() instead of int() to convert the input value:
 > getftemp = float(raw_input("enter fahrenheit temperature: "))
 >
 > Kent
 >

-- 
Regards,
erimendz ** use firefox/thunderbird **
http://www.spreadfirefox.com/?q=affiliates&id=6670&t=58



More information about the Tutor mailing list