0% found this document useful (0 votes)
2 views

Practical Exam_85 minutes_Python 4th

The document provides instructions for completing an assignment using the Thonny IDE, requiring the submission of a zip file containing three Python programs. The tasks include creating a number guessing game, processing a text file to find common words, and implementing an object-oriented program to manage employee data. Strict adherence to the guidelines is emphasized, with penalties for any violations.

Uploaded by

tranqm2006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical Exam_85 minutes_Python 4th

The document provides instructions for completing an assignment using the Thonny IDE, requiring the submission of a zip file containing three Python programs. The tasks include creating a number guessing game, processing a text file to find common words, and implementing an object-oriented program to manage employee data. Strict adherence to the guidelines is emphasized, with penalties for any violations.

Uploaded by

tranqm2006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

INSTRUCTIONS

Please read the instructions carefully before doing the


questions.
● You can use materials in your computer, notebook and
textbook.
● You are NOT allowed to use any device to share data
with others.
Besides the above conditions, students must follow the
following requirements:
1.The work must be completed by using Thonny IDE.
2.The result is three files .py, compressed into a
RollNo_Name_PFP191.zip (1), e.g.,
HE123456_DatNTT_PFP191.zip.
3.THIS PART IS VERY IMPORTANT, PLEASE READ IT CAREFULLY
AND FOLLOW THE INSTRUCTIONS.
● You are given a data text file.
● You cannot use other IDEs such as VS Code, PyCharm,
etc.
● If one of the above requirements is violated, your
work will be invalid.
On completion, submit the zip file (1).
85 minutes
1/ Write a program that uses the random function to
make the computer guess the number you think it is. For
example, if you think in the beginning of the number
68, the computer will first guess 15, you enter the
answer as l (low), the machine continues to guess 87,
you will enter h (high), the computer will try again.
continue guessing. And so on until the machine guesses
the number you think in your head is 68, then you enter
c (correct). The program will notify that the machine
guessed correctly and exit.
(3 points)
2. Write a program in Python that reads a text file
"data_text.txt" (Attached with exam question paper)
into a dictionary. Convert content from dictionary to
List. Sort the list and find in it the top 20 most
common words, the most appearing words, the least
appearing.
(3 points)
3. Write a program in the python programming language
using object-oriented programming techniques. The
program enters a list of employees, each with
information on name, age and salary. Sort employees by
decreasing age. Print the list of employees before and
after the sorting.
(4 points)
Solution:
1. Code
import random
def guess(x):
random_number = random.randint(1,x)
guess = 0
while guess != random_number:
guess = int(input(f'Guess a number between 1
and {x}: ‘))
if guess < random_number:
print(‘Sorry, guess again. Too low.’)
elif guess > random_number:
print(‘Sorry, guess again. Too high.’)
print(f'Well, congratulations. You have guessed the
number {random_number} correctly)
def computer_guess(x):
low = 1
high = x
feedback = ‘’
while feedback != ‘c':
if low != high:
guess = random.randint(low,high)
else:
guess = low
feedback = input(f'Is {guess} too high (H), too
low (L), or corect (C))
if feedback == “h":
high = guess - 1
elif feedback == “l”:
low = guess + 1
print(‘Welldone! The computer guessed your number,
{guess}, correctly!’)
computer_guess(1000)
2. Code
fhand = open(data.txt')
counts = {}
for line in fhand:
words = line.split()
for word in words:
counts[word] = counts.get(word, 0 ) + 1
lst = []
for key, val in counts.items():
newtup = (val, key)
lst.append(newtup)
lst = sorted(lst, reverse=True)
for val, key in lst[:20] :
print(key, val)

3. Code
class employee:
age = 0
salary = 0
name = ''
def set_age(self):
self.age = input('Enter age: ')
def get_age(self):
return self.age
def set_salary(self):
self.salary = input('Enter salary: ')
def get_salary(self):
return self.salary
def set_name(self):
self.name = input('Enter name: ')
def get_name(self):
return self.name

lst=[]
for i in range(5):
print('Enter employees :{}'.format(i+1))
tmp_st = employee()
tmp_st.set_name()
tmp_st.set_age()
tmp_st.set_salary()
lst.append(tmp_st)

print('before sorting:')
for i in range(5):
print('Employees {}'.format(i+1))
print('Name : {}'.format(lst[i].get_name()))
print('Salary : {}'.format(lst[i].get_salary()))
print('Age : {}'.format(lst[i].get_age()))

lst.sort(key=lambda x: x.age, reverse=True)

print('after sorting:')
for i in range(5):
print('Employees {}'.format(i+1))
print('Name : {}'.format(lst[i].get_name()))
print('Salary : {}'.format(lst[i].get_salary()))
print('Age : {}'.format(lst[i].get_age()))

def calculate_series_sum(n):
total = 0
for i in range(1, n + 1):
if i % 2 == 1:
total += i**2
else:
total -= i**2
return total

def main():
print("1. Find the sum of the series S = 1 -
2^2 + 3^2 - ... + n^2 (1 point)")
print("2. Option 2 (not implemented)")
print("3. Option 3 (not implemented)")
selection = input("Your selection (1 -> 3): ")

if selection == "1":
n = int(input("Enter the number of terms:
"))
result = calculate_series_sum(n)
print("OUTPUT")
print(result)
print("FINISH")

if __name__ == "__main__":
main()

You might also like