Uploaded by Jagannath Shrestha

softwarenow exam2021

advertisement
softwarenow exam:2021
question 1: raw coding
from tkinter import *
root = Tk()
class Student:
def __init__(self):
self.name = ""
self.email = ""
self.enrollment_status = ""
self.course_enrolled = ""
class StudentEnrollmentSystem:
def __init__(self, root):
self.root = root
self.root.title("Student Enrollment System")
self.student = Student()
# Create buttons
button1 = Button(root, text="Button 1 (Student Name / Email)", command=self.get_student_info)
button2 = Button(root, text="Button 2 (Enrollment Status)", command=self.get_enrollment_status)
button3 = Button(root, text="Button 3 (Course Enrolled)", command=self.assign_course)
button4 = Button(root, text="Button 4 (Exit)", command=self.quit)
# Create grid
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=1, column=0)
button4.grid(row=1, column=1)
def get_student_info(self):
self.student.name = input("Enter Student Name: ")
self.student.email = input("Enter Student Email: ")
def get_enrollment_status(self):
self.student.enrollment_status = input("Enter Enrollment Status (full time / part time): ")
def assign_course(self):
self.student.course_enrolled = input("Enter Course Enrolled: ")
def quit(self):
self.root.destroy()
if __name__ == "__main__":
app = StudentEnrollmentSystem(root)
root.mainloop()
question 2:
It appears that Marsha's intention was to create a loop that starts with the value of i set to 20 and prints the value of i in each iteration while making some modifications to i. It looks like she intended to divide i by 2 if it's even (i % 2 == 0) and increment i by 1 if it's odd. The loop continues until i becomes less than or equal to 0.
However, there are a few mistakes in the code:
Missing parentheses for the print statement in Python 2.
The indentation of the print statement is not aligned properly.
The loop should run until i is greater than 0, but the code currently runs while i > 0, which means it will exit when i becomes 0.
Here's a corrected version of Marsha's code:
python code:
n = 20
i = 20
while i > 0:
print(i) # Added parentheses for the print statement
if i % 2 == 0:
i = i // 2 # Use integer division (//) to ensure i remains an integer
else:
i = i + 1
With these changes, the code should correctly print the values of i and modify it as described in Marsha's intention until i becomes less than or equal to 0.
alternavtive solution:
It seems that Marsha's code has several issues, and it's not entirely clear what her intention was. Let's analyze the code:
python
Copy code
n = 20
i = 20
while i > 0:
print i
if i % 2 == 0:
i = i / 2
else:
i = i + 1
Based on the code, it appears that Marsha might be trying to perform some kind of iterative process with the variable i. The code starts with i and performs the following actions:
If i is even, it is divided by 2.
If i is odd, 1 is added to it.
The loop continues as long as i is greater than 0.
Without more context, it's unclear what the intended purpose of this code is. However, I can suggest two possible interpretations and corresponding fixes:
Interpretation 1: Marsha might be trying to perform a sequence of transformations on i until it reaches a certain condition. Let's assume she wants to count the number of iterations required for i to reach 1 while printing each step.
python
Copy code
n = 20
i = 20
iteration = 0 # Initialize a variable to count the number of iterations
while i > 1: # Changed the condition to reach 1
print(i)
if i % 2 == 0:
i = i // 2 # Use integer division
else:
i = i + 1
iteration += 1 # Increment the iteration count
print("Iterations:", iteration) # Print the total number of iterations
Interpretation 2: Marsha might want to print a sequence of numbers, reducing i by half if it's even, and increasing it by 1 if it's odd until reaching a certain condition.
python
Copy code
n = 20
i = 20
while n > 0: # Changed the loop variable to 'n'
print(i)
if i % 2 == 0:
i = i // 2 # Use integer division
else:
i = i + 1
n -= 1 # Decrement 'n' to control the loop
The choice between these interpretations depends on Marsha's actual intent. She should clarify her intention and make the necessary corrections to the code accordingly.
Download