First Python Program

• 01-20-2006
linuxuser40351
First Python Program
I just started teaching myself python like yesterday, and I've only read chapter 1 in my Computer Science book, so please excuse me if this question is rather noobish.

I've successfully written my first program, it converts Farenheit to Celsius and Celsius to Farenheit, and if you follow the on screen directions, the program works perfectly every time with no errors in any of its intended functions and responses. I'm using it as kind of an experiment to teach myself with, however, I have ran into a problem, first though, here is the current code for the program:
Code:

#File: tempconverter.py
#A simple program to convert Farenheit to Celsius and visa versa
#This is the first originally written program coded by Marcus Dean Adams.
def main():
print "Welcome to Marcus Adams's 2 way temperature converter."
y=1
n=0
z=input("Press 1 to convert Farenheit to Celsius, 2 to convert Celsius to Farenheit, or 3 to quit:")
if z!=1 and z!=2 and z!=3:
e=input("Invalid input, try again?(y or n)")
if e==1:
t=''
main()
if e==0:
t="Thank you for using Marcus Adams's 2 way temperature converter."
if z==1:
x=input("Input temperature in Farenheit:")
t=(x-32)/1.8
print "The temperature in Celsius is:"
if z==2:
x=input("Input temperature in Celsius:")
t=(x*1.8)+32
print "The temperature in Farenheit is:"
if z==3:
t="Thank you for using Marcus Adams's 2 way temperature converter."
print t
if z==1 or z==2:
a=input("Do you want to perform another conversion?(y or n)")
if a==0:
t="Thank you for using Marcus Adams's 2 way temperature converter."
if a==1:
t= ''
main()
print t

main()

Now, my next goal is to make it crash proof so that no matter what kind of response the user gives at any of the prompts, they will receive some kind of pre-determined output rather than the program crashing. Currently I have fixed one crash possibility that would occur if you entered a number other than 1, 2 or 3 in the first prompt. I fixed this by adding the if z!=1 and z!=2 and z!=3 statement. However, if at this prompt they enter a letter instead of a number, the program still crashes, however the only way I can think of to keep this from happening would be to go to the top of the file and set every single letter of the alphabet equal to a number other than 1, 2 or 3. I've tested this theory with a couple of letters and it does work, however if I did that the file would probably at least double in size, which isn't too much when you are talking less than a 1 KB file, but I'm curious, is there an easier way to do this? Also to make it 100% crash proof I would have to define every single letter, and to do so would mess with the ones I have set as variables later in the program.(I think) I expected the if z!=1....etc statement to apply to anything entered in that was not the numbers 1, 2 or 3, but it only applies to numbers and not letters. Does anybody know how I could fix this?
• 01-20-2006
jaboua
I think this post should be moved to the linux programming section.
• 01-21-2006
linuxuser40351
Oh I didn't see the Linux programming section, sorry, mods can move it if they see it.
• 01-21-2006
linuxuser40351
I have managed to make the program mainly crash free by using raw_input and specifying the input with quotes so it looks specifically for that letter and I don't have to set the letters equal to numbers. The new sourcecode for the program is:
Code:

#Marcus Adams's Temperature Converter V. 2.2
#A simple program to convert Fahrenheit to Celsius and vise versa
#This is the first originally written program coded by Marcus Dean Adams.
def main():
print "Welcome to Marcus Adams's 2 way temperature converter."
z=raw_input("Press 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit, or 3 to quit:")
if z=='1':
x=input("Input temperature in Fahrenheit:")
t=(x-32)/1.8
print "The temperature in Celsius is:"
elif z=='2':
x=input("Input temperature in Celsius:")
t=(x*1.8)+32
print "The temperature in Fahrenheit is:"
elif z=='3':
t="Thank you for using Marcus Adams's 2 way temperature converter."
else:
e=raw_input("Invalid input, try again?(y or n)")
if e=='y' or e=='Y':
t=''
main()
elif e=='n' or e=='N':
t="Thank you for using Marcus Adams's 2 way temperature converter."
else:
t="You have provided invalid input several times now, the program will now close."
print t
if z=='1' or z=='2':
a=raw_input("Do you want to perform another conversion?(y or n)")
if a=='n' or a=='N':
t="Thank you for using Marcus Adams's 2 way temperature converter."
elif a=='y' or a=='Y':
t= ''
main()
else:
t="You have provided invalid input, the program will now close."
print t

main()

I still have one crash possibility now, I couldn't think of an else statement to use on the prompts that ask them to input a temperature because the range of working and proper possibilities (basically any conceivable number) is too wide. Is there a way I could write an if or else statement that would exclude anything that is not a number? Also, how could I get the program to just return to line 5 and start over instead of running main inside itself to start over if they answer yes they want to perform another conversion? This is not really a "problem" but if you run several conversions before telling the program you are done and to quit, it inserts a number of blank lines after the goodbye statement.