PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 2
You are creating a simple calculator application using Tkinter in Python. The calculator has buttons for digits (0-9), basic arithmetic operations (+, -, *, /), and an "Equals" button to compute the result. The calculator displays the input and output in a Label widget. The code snippet below handles the arithmetic operations:
- import tkinter as tk
- def click_button(value):
- current_text = label_var.get()
- new_text = current_text + str(value)
- label_var.set(new_text)
- def calculate():
- try:
- result = eval(label_var.get())
- label_var.set(str(result))
- except Exception:
- label_var.set("Error")
- root = tk.Tk()
- label_var = tk.StringVar()
- label = tk.Label(root, textvariable=label_var)
- label.pack()
- buttons = [
- ('7', 1, 0), ('8', 1, 1), ('9', 1, 2),
- ('4', 2, 0), ('5', 2, 1), ('6', 2, 2),
- ('1', 3, 0), ('2', 3, 1), ('3', 3, 2),
- ('0', 4, 0), ('+', 1, 3), ('-', 2, 3),
- ('*', 3, 3), ('/', 4, 3), ('=', 4, 2)
- ]
- for (text, row, col) in buttons:
- button = tk.Button(root, text=text, command=lambda t=text: click_button(t))
- button.grid(row=row, column=col)
- equal_button = tk.Button(root, text='=', command=calculate)
- equal_button.grid(row=4, column=2)
- root.mainloop()
Which of the following statements is correct regarding this calculator implementation?
-
A
The
eval()function is a safe and efficient way to compute the result of the expression in thecalculate()function. -
B
The
commandparameter in the button creation loop is incorrectly usinglambdaand will cause all buttons to behave the same. -
C
The
click_button()function should use theappend()method instead of concatenation to add the value tonew_text. -
D
The
label_var.get()in thecalculate()function correctly retrieves the current expression as a string.
Reveal correct answer
Correct answer: D
A.
While eval() can evaluate the expression, it is not considered safe because it will execute any arbitrary code. In a real-world application, this could be exploited to run harmful commands. A safer approach would be to parse and evaluate the expression manually or use a specialized library like ast.literal_eval for safer evaluation.
B.
The lambda used in the button creation loop is correct. It ensures that each button passes its corresponding text value (t) to the click_button() function. Without lambda, all buttons would pass the same value (the last value in the loop), which is a common pitfall in Tkinter button creation.
C.
The append() method is not applicable here because current_text and new_text are strings, not lists. String concatenation is the correct method to append one string to another.
D.
The label_var.get() method is the correct way to retrieve the current string displayed in the Label widget. This string is then evaluated by the eval() function in the calculate() function.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
