提问人:user3671171 提问时间:8/21/2023 最后编辑:M. Justinuser3671171 更新时间:10/4/2023 访问量:42
随机选择 - 加权误差
Random Choices - Weighting Errors
问:
我正在尝试建立一个用于录取的抽签系统,但代码不断抛出权重错误:代码附在下面 -
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
import random
class SchoolAdmissionsApp:
def __init__(self, root):
self.root = root
self.root.title("School Admissions Lottery System")
self.num_students_label = tk.Label(root, text="Enter number of students to select:")
self.num_students_label.pack()
self.num_students_entry = tk.Entry(root)
self.num_students_entry.pack()
self.num_students_button = tk.Button(root, text="Set Number of Students", command=self.set_num_students)
self.num_students_button.pack()
self.load_button = tk.Button(root, text="Load Excel File", command=self.load_excel_file, state=tk.DISABLED)
self.load_button.pack()
self.select_button = tk.Button(root, text="Select Students", command=self.select_students, state=tk.DISABLED)
self.select_button.pack()
self.selected_students_window = None
self.num_students = 0
self.df = None
def set_num_students(self):
try:
self.num_students = int(self.num_students_entry.get())
if self.num_students <= 0:
raise ValueError
self.load_button.config(state=tk.NORMAL)
self.select_button.config(state=tk.DISABLED)
messagebox.showinfo("Number of Students", f"Number of students set to {self.num_students}")
except ValueError:
messagebox.showerror("Error", "Please enter a valid number")
def load_excel_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
if file_path:
try:
self.df = pd.read_excel(file_path)
self.select_button.config(state=tk.NORMAL)
messagebox.showinfo("Excel File Loaded", "Excel file loaded successfully")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the file:\n{str(e)}")
def select_students(self):
if self.num_students == 0 or self.df is None:
messagebox.showerror("Error", "Please set the number of students and load the Excel file")
return
try:
selected_students = self.process_data(self.df)
self.display_selected_students(selected_students)
except Exception as e:
messagebox.showerror("Error", f"An error occurred:\n{str(e)}")
def process_data(self, dataframe):
boys = dataframe[dataframe['Gender'] == 'M']
girls = dataframe[dataframe['Gender'] == 'F']
total_students=self.num_students
boys_count = girls_count = self.num_students // 2
selected_boys = boys.sample(min(boys_count, len(boys))).reset_index(drop=True)
selected_girls = girls.sample(min(girls_count, len(girls))).reset_index(drop=True)
age_dist = [0.25, 0.25, 0.25, 0.25]
age_range = range(3, 5)
selected_ages = random.choices(age_range, weights=age_dist, k=self.num_students)
selected_students = pd.concat([selected_boys, selected_girls], ignore_index=True)
# selected_students = pd.DataFrame({
##'No': selected_boys['No'].tolist() + selected_girls['No'].tolist(),
## 'Gender': selected_boys['Gender'].tolist() + selected_girls['Gender'].tolist(),
## 'Age': selected_ages
return selected_students
def display_selected_students(self, selected_students):
if self.selected_students_window:
self.selected_students_window.destroy()
self.selected_students_window = tk.Toplevel(self.root)
self.selected_students_window.title("Selected Students")
text = tk.Text(self.selected_students_window)
text.pack()
text.insert(tk.END, selected_students.to_string(index=False))
def main():
root = tk.Tk()
app = SchoolAdmissionsApp(root)
root.mainloop()
if __name__ == "__main__":
main()
有人能指出我哪里出了问题吗??
我希望输出包含 M 和 F 之间的等权重结果,并且也平均分布在代码中定义的年龄范围的四分之一中。
答: 暂无答案
评论