[Tutor] Function problem

Eri Mendz jerimed at myrealbox.com
Fri Nov 5 19:35:53 CET 2004


Hello All,

First off, thanks again to all who answered my loop problem Re: 
'IndexError....'. I got it understood now.

The problem of my script below is in the proceed function: if Y is
selected, instead of staying in the option menu, the program just shows 
the option menu then exits. ditto in the 'really quit program' prompt: i 
expect to go to main menu if i select N but again program exits. can you 
guys point me to my error? this is just a simple script but it took me 
some time to figure out on my own; its a good learning exercise 
nevertheless.

what to do to improve the program further, e.g., make it simple but
efficient? i love to hear your good advise.

#!/usr/bin/env python
# filename: tempConvert.py
# description: temperature conversion program
# Author: Eri Mendz
# Fri Nov  5 13:53:16 AST 2004

import sys

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]: ")
     if proceed == 'y' or proceed =='Y':
         print_options()
     else:
         confirmQuit()

def confirmQuit():
     ask = raw_input("Really quit program? [Y|N]: ")
     if ask == 'y' or ask == 'Y' or ask == 'ye' or ask == 'yes':
         sys.exit("bye")
     else:
         print_options()

# begin
while 1:
     choice = raw_input("select options: ")
     if choice == 'c' or choice == 'C':
         try:
             getftemp = int(raw_input("enter fahrenheit temperature: "))
             print "temp in C is: ", c2f(getftemp)
             proceed()
         except ValueError:
             print "error: not a valid input!"
         break
     elif choice == 'f' or choice == 'F':
         try:
             getctemp = int(raw_input("enter centigrade temperature: "))
             print "temp in F is: ", f2c(getctemp)
             proceed()
         except ValueError:
             print "error: not a valid input!"
         break
     elif choice =='p' or choice == 'P':
         print_options()
     elif choice =='q' or choice == 'Q':
         confirmQuit()
     else:
         print "invalid option"

print "bye"



More information about the Tutor mailing list