[go: up one dir, main page]

0% found this document useful (0 votes)
13 views7 pages

Artificial Intelligence Lab1

The first code demonstrates the implementation of a basic calculator with error handling for invalid inputs. It provides essential arithmetic operations and a user-friendly interface for users to perform calculations. The second code effectively showcases the capabilities of Python in reading and processing text files.

Uploaded by

Atiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Artificial Intelligence Lab1

The first code demonstrates the implementation of a basic calculator with error handling for invalid inputs. It provides essential arithmetic operations and a user-friendly interface for users to perform calculations. The second code effectively showcases the capabilities of Python in reading and processing text files.

Uploaded by

Atiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Question 1: Make a Calculator with basic functions. Flow Chart: Seles Operain( Assan Mitekcabon, Scsvacton iy it inion >> 5] perm nein Display result ZS a reer Display rau eon esa out 4 aon orto Ovens |]-—af ay na a Display rosuit re Operation) Came} ey Sercrcenrs) Pe ary c i ae) seo iene) ony r re oe) ee a ny ome ony t ¢ c et ere ee) See ae) Cee eae a eereyy ee Cent] Enter First Number Enter Second Wumber: Product of 5.0 and 3.0 1s 2.0 Sede! c calculator functions are added. Addition, Subtraction, Multiplication, Division and remainder Calculation, The program displays a welcome message and informs the user of the available operations and exit. The code enters a while loop that allows the user to perform calculator operations repeatedly until they choose to exit. The user is prompted to select an operation by entering a number (1, 2, 3, 4, 5, or 6). The code validates the user's input to ensure it's one of the valid operation options. If the input is invalid, the program asks the user to re-enter a valid option, If a valid operation is selected, the program prompts the user to enter two numbers. It uses a try...except block to handle potential input errors and continues the loop if the input is invalid. Based on the selected operation, the program calls the corresponding calculator function and displays the result. After performing an operation, the user is asked if they want to perform more operations. If they choose "No," the program exits the loop and displays a thank-you message. Otherwise, the user can continue performing calculations. Question 2. Write a string of text(3- 4 lines) In a file, save the file. Open the file and count the number of characters, words and lines in the file. Flow Chart: Open Fle L (Count Total Word I Count Total Characters I ‘Count Total Lines I Cioee File Solution Code: erect erie oon? r Gomer sy) peers (readoata) ented festeeerse Discussion: The code starts by defining the path to a text file, this path points to a file located on the desktop. A file is opended in read-only mode using the open function and the defined file path. A success message is printed to confirm that the file has been opened successfully. The seript reads the entire content of the file into the variable using the read() method. The total number of words are calculated in the file by splitting the contents based on spaces and then using the len()function to count the resulting list of words. The word count is printed to the console. The total number of characters are counted in the file using the len() function on the entire content stored in the variable. The character count is printed to the console. To count the total number of lines in the file, it opens the file again using a context manager ( with statement) and a file pointer (fp). It iterates through each line in the file and increments a counter for each line. The total line count is then printed to the console. After performing all the necessary operations, the script closes the file using the close()method. A message is printed to confirm the successful closure of the file. 6 Question 3. Make a class of employee, with name, employment number, Year of joining and designation. Add 10 employees record. Make a function to calculate the salary of any employee as per following formula, Salaray= Basic + 5000* number of years in the company. Flow Chart: Solution Code: tere ee Pes i Ret rae ates Rereper 7) The provided code defines a Python class called Employee and uses it to create instances of employees, calculate their salaries, and display their information, A class named Employeeis defined, which serves as a blueprint for creating employee objects. The init method is used for initializing the attributes of each employee when an instance is created. The attributes include the employee's name, employee number, year of joining the company, designation, and basic salary. This salary method calculates the salary of an employee based on their years of service and basic salary. The code initializes an empty list called EMPto store employee data. A forloop is used to iterate through the EMP list, which contains employee instances. For each employee in the list, the SalaryCalmethod is called to calculate the salary based on their years of service and basic salary Conclusion. In summary, the first code successfully demonstrates the implementation of a basic calculator with error handling for invalid inputs. It provides essential arithmetic operations and a user-friendly interface for users to perform calculations. The code can be enhanced further by adding more functionalities or a graphical user interface (GUI) for a more user- friendly experience, The second code effectively showcases the capabilities of Python in reading and processing text files. It accurately counts the number of words, characters, and lines within the specified text file, providing valuable insights for text analysis tasks. This code can be useful for tasks such as text summarization, content analysis, and quality assessment in various applications. The third code exemplifies the creation of an Employee class, demonstrating the principles of object-oriented programming. It allows for the creation of instances representing individual employees, calculates salaries based on years of service, and displays employee information. This code can be extended to build more comprehensive employee management systems, making it a valuable foundation for HR and payroll applications.

You might also like