Python Programming Help, Python Coding Help| Python Programming Practice Set

Realcode4you provide professional and quality python code with proper comments and explanation. Here you can get help in cheap price with full coding support. In this blog we have put the some advance level python coding questions in which you can practice to improve coding skills. If you need help in these question then hire expert which provide full support to practice of these questions with an affordable price.


Practice Set 1(Election Results)

The purpose of this task is to provide practice with reading data from a text file and using lists and loops to iterate through the data. You may find string functions like .split(“,”) to be helpful in parsing out the data being read.


Description:

Student body elections were recently held with the following candidates on the ballot (shown here in Python lists!):

presCandidates = ["Alex Abernathy", "Brooklyn Bianco"]

vpCandidates = ["Chris Cortez", "Darcy Delaney","Emery Evans"]

secCandidates = ["Pat Peters", "Robin Ramirez", "Sam Smith", "Tracy Turner"]


A programming student had the brilliant idea of writing a program for students to vote on a computer and store their choices in a test file, whose format is each line representing the president, vice president, secretary voted for as a commadelimited string. The text file looks like that shown at the right (except there are over 300 lines): But the student can’t figure out how to read the data into a Python program and have it tally the ballot and display the results. Can you help the student? The election.txt data file is available in the Canvas Assignment for downloading.

Write an original Python program that reads the data file and presents the results and use “def” functions: Many ways to do this, but here is my approach: Read the data file one line at a time (.readline()) in a while loop. I’d recommend removing the “\n” character a the end of the read string, and then split it by the comma (lookup the .split( ) string method. Split puts the items into a list). Then compare the result list item to the candidates; names and increment the appropriate variables (or items in an integer list) to keep track of votes received by each candidate. In the output, show the votes cast for each candidate and the total votes cast:











  • Save your file with as “CIS156_05C_Election Results_YourLastName.py”

  • Print the project tile on the first line. Print the next line showing your name as the developer, followed by a blank line (remember you can use \n). Then provide a description of what the program does.

  • You are limited to using features discussed thus far in class or in the assigned readings/videos to date. Thus you may not use custom classes to solve the problem, but loops, conditional structures, variables, lists, string methods and read/write methods can all be used in your code.


Practice Set 2(Movie Database)

This assignment provides practice with creating a custom class, encapsulating attributes, creating instances of the class and adding to a list, and providing for sorting class instances and outputting a print string using te__lt__ and __str__ methods.. It also revisits the use of methods (functions) and reading data from a text file from last week’s lesson.


Description:

Create a Python program that reads movie data from a text field (movies3.txt). The data is in a vertical pipe delimited format (title | year | director | genre | rating | cast). The class is stored in an external Python file (movie.py). The main() function of the program file imports the movie.py by calling another function which handles that and presents a menu to the user, allowing them to make a task choice and handles that choice. Each choice task should be handled by another called method. Here is a screen shot:








Choice A will list all the movies in the data collection (stores in class instances within a list):













Choice 1 will sort the list alphabetically (A-Z) by title using the __lt__ method of the class:




Now re-running

Choice A will show all the movies in Alphabetical order.











Likewise, choice 2 sorts the list chronologically by year (oldest to newest). This sort is done directly from the main program by using the list’s sort command with a lambda expression that calls one of the class’s accessor methods , i.e. get_year() as the sort key: library.sort(key=lambda movie: movie.get_year())




Now re-listing the movies (Choice A) provides a list sorted chronologically:












Choice B lists movies that match a provided genre. NOTE: the order will be whatever the last sort was –alphabetical, chronological or unsorted (if no order was applied after importing):










Choice C provide a list of all movies whose director contains a provided name (including partial): (NOTE: the order will be whatever the last sort was –alphabetical, chronological or unsorted (if no order was applied after importing)












Choice D is very similar to choice C, but provides a list of all movies that contain a provided actor/actress name (including partial): (NOTE: the order will be whatever the last sort was –alphabetical, chronological or unsorted (if no order was applied after importing):













Choice ? re-displays the user menu:








And Choice X causes the program to terminate:




  • Save your files as “movie.py” and “CIS156_06A_Movie_Database_YourLastName.py”

  • Use the movies3.txt file provided in the Canvas site (Assignments). It is vertical Pipe “|” delimited in the format of title | year | director | genre | rating | cast with a header row.

  • Create a .py file for the Movie class with an intitializer ( __init__ ) that accepts the title, year, director, genre, rating, and cast data fields. Use encapsulation to protect each of the raw data fields, but provide accessor and mutator methods to access and modify the data, even though not all will be used. Incorporate the __lt__ method to create a sort on the title data and a __str__ method to return a string with all the info of the movie in the format: TITLE (year) Genre [rating] Director: directorName Cast list Note the examples in the screen shots above.

  • In the “CIS156_06A_Movie_Database_YourLastName.py” file, import the movie.py file to be able to access the Movie class.

  • Use a while loop in main() to continue the program until the user chooses to exit (choice X)


Practice Set 3(Triangle Calculator)

This assignment utilizes the tkinter, the GUI programming toolkit for Python. This is a revisiting of our first assignment, but now creating it in as GUI application.


Description:

Create a GUI-based application using tkinter that accepts the lengths of sides A and B from the user via Entry widgets and upon clicking a button calculates and displays the length of the hypotenuse and the area and perimeter of the triangle (the sum of the three sides), all three formatted with two decimal places. The client also would like a visual of a triangle in the application with the widgets for each side distributed around it as shown below (side A on the left, side on the bottom and the hypotenuse on the right.)











REQUIREMENTS AND TIPS:

  • Use frames! After the title label, I created a frame for the other controls. That frame has three nested frames – a left frame (for side A controls), a center frame (for the Canvas (containing the triangle graphics) and the side B widgets, and a right frame containing labels for the hypotenuse, area, and perimeter as well as the Calculate button.

  • Remember that to get items to display horizontally, you should use the side=’left’ argument in the pack( ) method.

  • I use blank labels to provide space either horizontally or vertically between controls. For instance I put a small lable to the left of the left frame.

  • If you are going to update a widget either with additional configuration code or programmatically (such as displaying output in a label), pack the widget separately rather than in its declaration statement

  • Use the StringVar( ) instance of tkinter to create variable for initial text display in an Entry:

self.sideA = tkinter.StringVar() 
self.sideA.set("12")  
self.entryA = tkinter.Entry(self.frame1left, width=8, bg="yellow", textvariable=self.sideA)
  • Use the get( ) method of each the Entry widgets to find their value for computational purposes.

  • Google how to draw lines in a Python 3 Canvas. self.imgCanvas.create_line(x1,y1,x2,y2)


Practice Set 4(RGB Color Mixer)

This assignment utilizes the tkinter, the GUI programming toolkit for Python.


Description:

In this assignment you will create a custom class to build a GUI interface consisting of labels, a Canvas (for the color display), and three Scale widgets (one for each of the red, green and blue values.) The project allows the user to create any of 16.7 million colors and display the resulting color in the Canvas. It should also display the Hexadecimal value of the 24 bit color (e.g. #FFFF00” would be pure yellow.)