- First Python Program - -------------------- - Write a Python program which acts as a simple calculator. The program should - accept several numbers, add them together, and then print the sum of the - numbers. - - You will need to use a new Python instruction to read from the keyboard. - This is similar to scanf in C. To read from the keyboard in Python you - will use raw_input(). This will make the program wait until the user - types in something at the keyboard and presses return. It looks like - this: - - number = raw_input() - - Where number is a variable. The value that the user enters will be stored - in the variable number. If you run the following Python program: - - #!/usr/bin/python - - number = raw_input() - print number - - It will wait until you type in something at the keyboard followed by - pressing the return (or enter) key. Everything you typed before the return - will be stored in the variable number. - - You can enter the program and try raw_input() if you want. - - If you save the above program in a file named lab4.py, run the program and - type in the string fred followed by the return key then you will see the - following: - - ./lab4.py - fred - fred - - The symbol means the return key. The first line is the user - executing the program. The second is what the user typed in. - The third is the output of the program. - - Step 1 - Loop and read input - ---------------------------- - To start writing the calculator you need a to write a loop which reads - the numbers from the user. The loop should execute until the user - types "exit". - - The program should have the following structure. Each line in the following - example represents one line of code. - -set the input variable equal to an empty string "" - -loop until the input variable is equal to "exit" using the while loop - -read input from the user using input = raw_input() - - Replace each line in the above example with a line of Python code. - The first line of the above program sets the variable equal to an - empty string which is a string containing nothing. This is represented - as two quotes with nothing between them "". - Don't forget to add #!/usr/bin/python as the first line. Also remember - to run chmod u+x lab4.py on the command line (assuming you name the file - lab4.py). - - If you run the above program it will not print anything to the screen. - You will only see the values the user enters. If you run it with - the inputs 1, 2, 3, and exit it should look like this: - - 1 - 2 - 3 - exit - - and then the program should stop. - - - Step 2 - Converting a string to a number - ---------------------------------------- - The next step involves converting the keyboard input into a number. - The value returned from raw_input() is a string. This means that you cannot - perform arithmetic with it. We need to convert it to a number. - To convert the string to a number you need to use the int() instruction. - - The int() instruction works like this: - - x = int("34") - - In this example the string "34" is converted to the number 34. - - Take the previous program and add a line to convert the input to an integer. - Add two lines to the above program. The new lines are marked with a * - in the following example. This first initializes a variable - which will store the total of all of the numbers added together. - The second new line will convert the input to an integer and add it - to the existing total. - -set the input variable equal to an empty string "" - -* set the total variable equal to zero (the integer 0) - -loop until the input variable is equal to "exit" using the while loop - -read input from the user using input = raw_input() - -* convert input to an integer and add it to the total variable - - - Step 3 - Exit correctly - ----------------------- - The above program wont exit correctly. This is because when you type - exit to end the program the last line attempts to convert the string "exit" - into an integer (total = int("exit")) and this cannot work. To stop this - we need to test if the input is equal to "exit" before the int() conversion - is used. Do this using an if statement before the last line which - tests if the input is "exit". If the input is not exit then add the input - to the total. - - The structure of the code looks like this: - -set the input variable equal to an empty string "" - -set the total variable equal to zero (the integer 0) - -loop until the input variable is equal to "exit" using the while loop - -read input from the user using input = raw_input() - -* if input is not equal to "exit" then perform then - -* convert input to an integer and add it to the total variable - -* print total - - Changes lines are marked with the *. The first new line is the new if - statement. The second changed line is simple indented under the if statement. - The last changed line is to print out the result stored in total. - - When the program is run it should look like this: - - ./lab4.py - 2 - 4 - 5 - exit - Total = 11 - - The user runs the program lab4.py. Then they enter the numbers 2,4,5 followed - by exit. The program adds the numbers and prints the total of them to the - screen. - - - The program is worth 3 marks. - -