Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org First Python Program
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 01-20-2006, 12:50 AM #1 dudeman41465 Member   Registered: Jun 2005 Location: Kentucky Distribution: Ubuntu Posts: 794 Rep: 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? Last edited by dudeman41465; 01-20-2006 at 12:54 AM.
 01-20-2006, 05:33 AM #2 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: ubuntu Posts: 2,530 Rep: Code: ```def main(): print "Welcome to Marcus Adams's 2 way temperature converter." y=1 # Note needed. y is never used somwhere. n=0 # Note needed. y is never used somwhere. # input() evaluates the user input (i.e. it runs the input as python code). This causes # an error if the user inputs 'A'. Python will then run 'A' as if it were python code. # 'A' is not known by your program as a variable, hence the errors. # # Solution: # Use raw_input() instead of input(). This will return the input as a string, no # matter what is was. then you can check whether the contents of the string are # valid input for your program. z = raw_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': # Note the quotes: z is a string now. e = raw_input("Invalid input, try again?(y or n)") if e=='y': t='' main() # This will run main() in main() in main() in ... # Not a good idea (in this case), but forget about this for now # while still learning the basics. if e=='n': t="Thank you for using Marcus Adams's 2 way temperature converter." if z=='1': x=raw_input("Input temperature in Farenheit:") x = int(x) # convert string to a number (int). t=(x-32)/1.8 print "The temperature in Celsius is:", # the comma will make the answer appear after this text. if z=='2': x=raw_input("Input temperature in Celsius:") x = int(x) # convert string to a number (int). 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=raw_input("Do you want to perform another conversion?(y or n)") if a=='n': t="Thank you for using Marcus Adams's 2 way temperature converter." if a=='y': t= '' main() # Calling main() inside main() itself, see commment above about this. print t main()```
 01-20-2006, 10:31 PM #3 dudeman41465 Member   Registered: Jun 2005 Location: Kentucky Distribution: Ubuntu Posts: 794 Original Poster Rep: I looked over some of the changes you made and thank you for your input, quoting the input and output and using raw_input prevents me from having to define the letters as numbers, it lets me define specifically what I want it to say. I also replaced some of those "if" statements with elif and else statements. I only have two more questions. 1.) Can I define somehow in the temperature input bars to only allow numbers? 2.) How can I re-route the lines that re-run main() to line 5 and just make it start over there instead of running main inside of itself? (i.e. if they answer yes they want to perform another conversion it just starts the program over at line 5)
01-21-2006, 03:22 PM   #4
carl.waldbieser
Member

Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep:
Quote:
 Originally Posted by dudeman41465 I looked over some of the changes you made and thank you for your input, quoting the input and output and using raw_input prevents me from having to define the letters as numbers, it lets me define specifically what I want it to say. I also replaced some of those "if" statements with elif and else statements. I only have two more questions. 1.) Can I define somehow in the temperature input bars to only allow numbers? 2.) How can I re-route the lines that re-run main() to line 5 and just make it start over there instead of running main inside of itself? (i.e. if they answer yes they want to perform another conversion it just starts the program over at line 5)
For both these situations, you normally want to make the input athering and validation inside a loop. For example:

Code:
```while true:
temp = raw_input("Please enter a temperature:")
if valid_temp(temp):
break
#continue processing

def valid_temp(temp):
try:
temp = int(temp)
except:
return False
if temp < -1000 or temp > 5000:
return False
return True```
For your main function, you can basically wrap the whole thing in a loop, and only end if the main function returns an exit code.