我有一个函数可以查找用户在GUI上输入的3个数字的平均值。我需要能够显示2小数点的答案。我只是找不到在哪里使用取整功能。
def average():
try:
value1 = int(month1_entry.get())
value2 = int(month2_entry.get())
value3 = int(month3_entry.get())
cases_average.set((value1 + value2 + value3) / 3)
except ValueError:
messagebox.showerror("Error", "Please Enter a Whole Number")
average_label = ttk.Label(frame, textvariable = cases_average)
average_label.grid(column = 2, row = 5, sticky = (E))
任何帮助将不胜感激
谢谢
答案 0 :(得分:1)
您可以使用round功能
def average():
try:
value1 = int(month1_entry.get())
value2 = int(month2_entry.get())
value3 = int(month3_entry.get())
cases_average.set(round((value1 + value2 + value3) / 3,2)) # I have changed here
except ValueError:
messagebox.showerror("Error", "Please Enter a Whole Number")