Jupyter Notebook中Dropdown组件异常:最后一个控件返回字典而非浮点数
Let's break down why your last dropdown is returning a dictionary instead of a float, and fix it step by step:
1. Typo in Dropdown Initialization
First, you made a small but critical typo when creating the SBARatioList dropdown: you used option (singular) instead of options (plural). ipywidgets relies on the plural options parameter to link your dictionary to the dropdown, so this mistake broke the value mapping for the last control.
2. Unnecessary Value Overwriting in the Callback
In your print_dropdown function, you're directly assigning Guarantee_Ratio = SBARatiod—this replaces the selected float value from the dropdown with the entire dictionary! The same error applies to variables like BusinessList = Businessd: you don't need to reassign these here, since the @interact decorator already passes the selected values from each dropdown into the function parameters.
Corrected Code
Here's the fixed version of your code that will return floats for all dropdowns:
# Initialise GUI from ipywidgets import interact, Dropdown import ipywidgets as widgets # Initialise Dictionaries NAICSd = {"21-Mining,Quarrying,and oil and gas extraction(8)":0.08, "11-Agriculture,forestry,fishing and hunting(9)":0.09, "55-Management of companies and enterprises(10)":0.08, "62-Healthcare and social assistance(10)":0.1, "22-Utilities(14)":0.14, "92-Public Administration(15)":0.15, "54-Professional,scientific and technical services(19)":0.19, "42-Wholesale trade(19)":0.19, "31-Manufacturing(19)":0.19, "32-Manufacturing(16)":0.16, "33-Manufacturing(14)":0.14, "81-Other Sevices Exept Public Administration(20)":0.2, "71-Arts,Entertainment and Recreation(21)":0.21, "72-Accommodation and Food Services(22)":0.22, "44-Retail Trade(22)":0.22, "45-Retail Trade(23)":0.23, "23-Construction(23)":0.23, "56-Administrative/Support & Waste Management/Remediation Service(24)":0.24, "61-Educational Services(24)":0.24, "51-Information (25)":0.25, "48-Transportation and warehousing(27)":0.27, "49-Transportation and warehousing(23)":0.23, "52-Finance and Insurance(28)":0.28, "53-Real Estate and rental and leasing(29)":0.29} Stated = {"Undefined(28)":0.28, "Alaska(10)":0.1, "Alabama(18)":0.18, "Arkansas(18)":0.18, "Arizona(20)":0.2, "California(20)":0.2, "Colorado(18)":0.18, "Connecticut(13)":0.13, "D.C.(8)":0.08, "Delaware(18)":0.18, "Florida(28)":0.28, "Georgia(27)":0.27, "Hawaii(16)":0.16, "Iowa(13)":0.13, "Idaho(15)":0.15, "Illonois(23)":0.23, "Indiana(18)":0.18, "Kansas(13)":0.13, "Kentucy(20)":0.20, "Louisiana(18)":0.18, "Massachusetts(15)":0.15, "Maryland(20)":0.20, "Maine(9)":0.09, "Michigan(23)":0.23, "Minnesota(13)":0.13, "Missouri(15)":0.15, "Missisipi(15)":0.15, "Montana(8)":0.08, "North Carolina(20)":0.2, "North Dakota(8)":0.08, "Nebraska(15)":0.15, "New Hampshire(10)":0.1, "New Jersey(20)":0.2, "New Mexico(10)":0.1, "Nevada(23)":0.23, "New York(20)":0.2, "Ohio(18)":0.18, "Oklahoma(13)":0.13, "Oregon(17)":0.17, "Pennsylvania(15)":0.15, "Rhode Island(8)":0.08, "South Carolina(20)":0.2, "South Dakota(8)":0.08, "Tennesee(23)":0.23, "Texas(20)":0.2, "Utah(18)":0.18, "Virginia(19)":0.19, "Vermont(8)":0.08, "Washington(13)":0.13, "Wisconsin(15)":0.15, "West Virginia(15)":0.15, "Wyoming(8)":0.18 } Businessd = {"New Business <2 years(18.98)":0.1898, "Normal Business >= 2 years (17.36)":0.1736} BackedRealEstated = {"Yes(1.64)":0.0164, "No(21.16)":0.2116} IsRecessiond ={"Yes(32.21)":0.3221, "No(16.63)":0.1663} SBARatiod = {"More than 50%(25)":0.25, "50% or less(40)":0.4} # Fix 1: Use "options" (plural) for SBARatioList NAICSList = Dropdown(options = NAICSd) StateList = Dropdown(options = Stated) BusinessList = Dropdown(options = Businessd) BackedRealEstateList = Dropdown(options = BackedRealEstated) IsRecessionList = Dropdown(options = IsRecessiond) SBARatioList = Dropdown(options = SBARatiod) @interact(Sector = NAICSList, US_State=StateList, New_Business=BusinessList, Real_Estate_Backed=BackedRealEstateList, Recession = IsRecessionList, Guarantee_Ratio = SBARatioList) def print_dropdown(Sector, US_State, New_Business, Real_Estate_Backed, Recession, Guarantee_Ratio): # Fix 2: Remove unnecessary dictionary reassignments # The function parameters already hold the selected float values print(Sector, US_State, New_Business, Real_Estate_Backed, Recession, Guarantee_Ratio)
Key Changes Explained
- Fixed the typo
option→optionswhen creatingSBARatioListso the dropdown correctly maps labels to their corresponding float values. - Removed all unnecessary reassignments inside the
print_dropdownfunction. The@interactdecorator automatically passes the selected value from each dropdown to the function parameters, so overwriting them with the full dictionary was breaking the output.
Now all your dropdowns will return the expected float values when you select an option.
内容的提问来源于stack exchange,提问作者user3823869

