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?