Python Programming Practice- A first program

Created with Sketch.

Python Programming Practice -A first program


Start IDLE and open up a new window (choose New Window under the File Menu). Type in the following program.

temp = eval ( input ( ' Enter a temperature in Celsius: ' ))
print ( ' In Fahrenheit, that is ' , 9/5*temp+32)


Then, under the Run menu, choose Run Module (or press F5). IDLE will ask you to save the file, and you should do so. Be sure to append .py to the filename as IDLE will not automatically append it. This will tell IDLE to use colors to make your program easier to read.

Once you’ve saved the program, it will run in the shell window. The program will ask you for a temperature. Type in 20 and press enter. The program’s output looks something like this:

Enter a temperature in Celsius: 20 
In Fahrenheit, that is 68.0


Let’s examine how the program does what it does. The first line asks the user to enter a tempera- ture. The input function’s job is to ask the user to type something in and to capture what the user types. The part in quotes is the prompt that the user sees. It is called a string and it will appear to the program’s user exactly as it appears in the code itself. The eval function is something we use here, but it won’t be clear exactly why until later. So for now, just remember that we use it when we’re getting numerical input.

We need to give a name to the value that the user enters so that the program can remember it and use it in the second line. The name we use is temp and we use the equals sign to assign the user ’s value to temp .

The second line uses the print function to print out the conversion. The part in quotes is another string and will appear to your program’s user exactly as it appears in quotes here. The second

Leave a Reply

Your email address will not be published. Required fields are marked *