StudentShare
Contact Us
Sign In / Sign Up for FREE
Search
Go to advanced search...
Free

Programming in Python - Assignment Example

Summary
The paper "Programming in Python" is an outstanding example of an information technology assignment. Function menu() is used to provide the options for the application. It is called by the main() function when the program starts. After providing the options to use for the applications, it prompts the user for the digit(s) representing the option they want to use. Eg. "1…
Download full paper File format: .doc, available for editing
GRAB THE BEST PAPER92.8% of users find it useful

Extract of sample "Programming in Python"

Programming in python SECTION A Question 1 Function menu() is used to provide the options for the application. It is called by the main() function when the program starts. After providing the options to use for the applications, it prompts the user for the digit(s) representing the option they want to use. Eg. "1. Display all the scheduled flights on file". When the user responds with a “1” the value is returned to the main() function and it displays all the scheduled flights after processing in the main() function. Question 2 In the line of code “outfile = open(DATA_FILE, 'a')” the ‘a’ is a parameter that is passed to in-built open() function to mean that any data should be appended to the end of the data file. In other words, add any new stream of text at the end of the already existing text in the data file. Question 3 FLIGHT_NO is a global variable and it represents 0 (zero). It is set/initialized at the beginning of the script and not within any function. It may then be used in any function without declaration and initialization inside the function. Question 4 In the second line in main(), choice = menu(), choice gets its value from the returned value in menu() function. The last line of menu() is “return option”, option being the value entered by a user after they were prompted to enter a number shown on the select menu. After, menu returns the value in variable option; it is then assigned to choice. Question 5 When the program is run and the user enters three, the menu() function return the value (3) and it is assigned to choice thus variable choice now represents 3 in the main() function. Variable choice is then checked using the while loop to ensure it is not equal to 20, 20 represent an exit from system run. Variable choice is then checked using the if – elif control structure, using the “choice == 3” the application prompt for a flight number in the main() function and the calls function displayFlightDetails(infile, flightNumber). The function then displays the flight details for a particular flight number. Question 6 When the program finishes, the value of choice is 20. Before variable choice attains this value, the user is prompted to enter an option. When the user enters 20, the variable choice is assigned with 20 and it checks in the while loop. The condition in the while loop permits continuous execution as long as the value of choice is not equal to 20. If the value is 20, it exits the while loop and the program terminates. Question 7 In the function displayFlightList, the variable splitLine contains an array of values obtained from splitting a particular line of text in the flight.txt. The spaces are used as delimiters to signify the end of one value and start of another. splitLine[ORIGIN] contains the origin of a given flight as stored in the line of text. ORIGIN contains the value 2 which gives the index of the array in which it will pick the value of flight origin. Question 8 Function eval() is used to evaluate some lines of code as a syntactic expression and return a result. In other words it is used to access the python interpreter and its parameters provide the lines of code to be interpreted and the result is then fetched from the from the returned value. It may be used when you need to execute some lines of code as a separate entity of the program and capture the results. Question 9 Python code for displayFlightDetails which calls the displayFlightLineDetails() function def displayFlightDetails(inf, flnumber): print "Details for flight:",flnumber for line in inf: splitLine = string.split(line) if splitLine[FLIGHT_NO] == flnumber: displayFlightLineDetails(line) return Question 10 In displayFlightList (inf), ‘inf’ refers to infile and this is the data that has been obtained from reading the flight.txt file. It contains all the text read from the file and can be accessed line by line. The “for line in inf:” line means that for each line of text in a particularof the data stored in inf, assign it to variable line and process. Question 11 The programming line after the while loop is infile.close(). This file is used to close the flight.txt file which was opened to read the data in it. This is because the open connection as an object is closed using the function close(). Question 12 In function “def displayFlightsFromLocn(inf, givenLocn, givenDay):’ parameter givenDay refers to the number of a given day of this week where Sunday is 1 to Saturday as 7. It gets its value from the main() function after a user is prompted to enter this value as a number between 1-7. The value is then assigned to variable “day” and it is passed to the function once called in main(). Question 13 In function findCheapestFlight(), minPrice has been used to store the value the minimum price in all the flights available. minPrice is first given a price that is more than the maximum anticipated price. Then a comparison of is made between this minPrice and the price of a given flight. If the price of the flight is less than the current minPrice, the value of the price is assigned to minPrice thus is becomes the new minimum. This is done for all the flight prices in the file thus at the end of the comparisons it is certain that the minimum value has been obtained. SECTION C Question 1 For the function calculateCostOfFlight(line) it will take one parameter “line” of data type string. This parameter carries a single line of the flight.txt file content and then line represents the flight details for one flight. The function returns one value “cost” which is extracted from the line. Pseudo code This is the pseudo code for the function calculateCostOfFlight(line) Define function calculateCostOfFlight which takes one parameter line Split the line flight details and get the price Assign the price of the flight to the variable cost. Return cost back to the main() function which called this function Question 2 For the functions findNumberOfFlightsToCity(inf, givenCity), it takes 2 parameters “inf” which is a pointer to the input file and given city which is of data type string. The function returns one value “count” which is an integer and it contains the number of flights that are supposed to have their destination in the city that was passed as a parameter. Pseudo code This is the pseudo code for function findNumberOfFlightsToCity(inf, givenCity): Define function findNumberOfFlightsToCity that take two parameters inf and givenCity Set the count of flights to zero For each line in the flight.txt file: Check if the destination city is same as the value contained in variable givenCity If values match increment count by one Return the value of count back to the main menu Question 3 For function addFlight(dataString), it takes a single parameter which is “dataString” and this is of data type string. The string contains values of the flight details for a given flight e.g. flight number, origin, destination e.t.c. This function does not return any values to the main() function that called is but its main purpose is to open the flight.txt file and append the flight details at the end of the file. Pseudo code The pseudo code for the addFlight(newString) function Define function addFlight that takes one paremeter newString Open the closed flight.txt file in append with mode ‘a’ for appending Write the string and add a new line character to move to the next line. Close the opened file Return null to go back to the main() function. Read More

CHECK THESE SAMPLES OF Programming in Python

Python Programming Language

python programming language includes various features of modern programming languages such as it is an interactive, explained, and programming language which works on objects.... python Programming Language.... python programming language includes various features of modern programming languages such as it is an interactive, explained, and programming language which works on objects.... In addition, python language integrates amazing control with extremely clear and simple language syntax or python programming rules....
15 Pages (3750 words) Essay

Object-Oriented Programming

This report "Object-Oriented programming" presents object-oriented programming that uses more time in program execution since the code is executed as a block.... However, procedural programming breaks down the function into simpler functions that are executed in an easier manner.... Both procedural programming and object-oriented programming allow code re-usage though the extent of re-use matters.... In object-oriented programming, code re-usage is far more effective than in procedural programming since object-oriented programming was designed to enable code re-usability....
8 Pages (2000 words) Report

Web technologies.From PHP to Python

All these can run on our home machine if we are just experimenting with PHP programming.... Furthermore, we also have the choice of using procedural programming or object oriented programming, or a mixture of them.... The distinguishing factor of PHP from client-side languages like JavaScript is that the code is executed on the server....
21 Pages (5250 words) Essay

The future of computer logic and programming with Python

in python, programs are written using an indentation technique.... Normally, in python, programs are written using an indentation technique (Bodnar, 2010).... This essay 'The future of computer logic and programming with python' discusses the role of the python programming language in today's programming paradigms.... This research shows how the future of the python programming language is secure.... The author states that python programming language was created by Guido van Rossum and this development was supported and encouraged by various other programming paradigms such as by ABC, Haskell, Java, LIST programming, Icon and Perl programming languages....
5 Pages (1250 words) Essay

Minimalism movement

The essay "Minimalism movement" explore the minimalism in music.... Many people mistakenly associate minimalism with the art of painting, while truth is that this principle has been applied for many years now in musical compositions created by a broad range of artists.... ... ... ... Minimalism is the name of an advanced movement which started in the world of music to popularize the use of very short and simple phrases....
5 Pages (1250 words) Essay

Python Programming Language

This report 'python Programming Language' presents a detailed overview of the python programming language.... This report will discuss some of the prime aspects of python programming language, its history, design goals, purpose and the relevance to Linux.... The author states that python is a well structured object-oriented programming language with dynamic functionalities.... The basic design principle of the python language highlights the programmer efficiency and program code interpretation....
12 Pages (3000 words) Essay

A Comparison of Programming Languages

in python, there is the Global interpreter lock while in Ruby, there is the Global VM lock.... These are various scripting languages which include Hypertext Pre-processor (PHP), Java Server Pages (JSP), Ruby on Rails, python among others.... In terms of supporting multithreading, ruby and python have been designed to support multithreading functionality.... The scripting languages are preferred over other programming languages since they are powerful, simple, and can easily be usedDespite the fact that a python is an object-oriented programming language, it started out as a procedural language which then incorporated object-oriented features....
5 Pages (1250 words) Coursework
sponsored ads
We use cookies to create the best experience for you. Keep on browsing if you are OK with that, or find out how to manage cookies.
Contact Us