Streamlit代码遇IndentationError错误,求解决方案
解决Python代码中的IndentationError(意外缩进)问题
错误分析
你遇到的IndentationError: unexpected indent是因为代码里存在三处缩进错误:
- 第131行的
color_mapping定义有多余缩进,它应该和外层if uploaded_file is not None:代码块内的其他同级代码保持一致缩进 calculate_percentage函数内的return overall_percentage缩进级别错误,它应该属于函数内部,目前和函数定义同级,导致函数未正确闭合- 函数内
# Calculate the percentage of horses that ran 4 times over hurdles下方的代码行有多余缩进
修正后的完整代码
import streamlit as st import pandas as pd # Create a file uploader widget uploaded_file = st.file_uploader("Choose an Excel file", type="xlsx") # Check if a file has been uploaded if uploaded_file is not None: # Load the Excel file into a Pandas dataframe df = pd.read_excel(uploaded_file) # Define the winning criteria for each stat age_criteria = (5, 6, 7, 8) rating_criteria = (139, float('inf')) improving_criteria = True won_last_race_criteria = True graded_win_criteria = True two_wins_this_season_criteria = True run_at_course_criteria = True top_3_in_betting_criteria = True two_wins_over_hurdles_criteria = True run_4_times_over_hurdles_criteria = True won_twice_over_15_17_furlongs_criteria = True ran_over_15_17_furlongs_criteria = True # Define a function to calculate the percentage of winning for each horse def calculate_percentage(row): age = row['Age'] rating = row['Rating'] improving = row['Improving'] won_last_race = row['Won Last Race?'] if won_last_race == 'y': won_last_race = True elif won_last_race == 'n': won_last_race = False graded_win = row['Graded Win?'] two_wins_this_season = row['Two Wins This Season?'] run_at_course = row['Run at Course?'] top_3_in_betting = row['Top 3 in Betting?'] two_wins_over_hurdles = row['Two Wins Over Hurdles?'] run_4_times_over_hurdles = row['Run 4 Times Over Hurdles?'] won_twice_over_15_17_furlongs = row['Won Twice Over 15-17 Furlongs?'] ran_over_15_17_furlongs = row['Ran Over 15-17 Furlongs?'] # Calculate the percentage of horses that meet the age criteria num_age_criteria_met = int(age in age_criteria) age_percentage = num_age_criteria_met * 100 * 9 / 11 # Calculate the percentage of horses that meet the rating criteria num_rating_criteria_met = int(rating >= rating_criteria[0]) rating_percentage = num_rating_criteria_met * 100 * 9 / 11 # Calculate the percentage of horses that won the last race num_won_last_race_criteria_met = int(won_last_race == won_last_race_criteria) won_last_race_percentage = num_won_last_race_criteria_met * 100 * 5 / 11 # Calculate the percentage of horses that had a graded win num_graded_win_criteria_met = int(graded_win == graded_win_criteria) graded_win_percentage = num_graded_win_criteria_met * 100 * 7 / 11 # Calculate the percentage of horses that had two wins this season num_two_wins_this_season_criteria_met = int(two_wins_this_season == two_wins_this_season_criteria) two_wins_this_season_percentage = num_two_wins_this_season_criteria_met * 100 * 8 / 11 num_run_at_course_criteria_met = int(run_at_course == run_at_course_criteria) run_at_course_percentage = num_run_at_course_criteria_met * 100 * 6 / 11 # Calculate the percentage of horses that had a top 3 finish in the betting num_top_3_in_betting_criteria_met = int(top_3_in_betting == top_3_in_betting_criteria) top_3_in_betting_percentage = num_top_3_in_betting_criteria_met * 100 * 7 / 11 # Calculate the percentage of horses that had two wins over hurdles num_two_wins_over_hurdles_criteria_met = int(two_wins_over_hurdles == two_wins_over_hurdles_criteria) two_wins_over_hurdles_percentage = num_two_wins_over_hurdles_criteria_met * 100 * 7 / 11 # Calculate the percentage of horses that ran 4 times over hurdles num_run_4_times_over_hurdles_criteria_met = int(run_4_times_over_hurdles == run_4_times_over_hurdles_criteria) run_4_times_over_hurdles_percentage = num_run_4_times_over_hurdles_criteria_met * 100 * 6 / 11 # Calculate the percentage of horses that won twice over 15-17 furlongs num_won_twice_over_15_17_furlongs_criteria_met = int(won_twice_over_15_17_furlongs == won_twice_over_15_17_furlongs_criteria) won_twice_over_15_17_furlongs_percentage = num_won_twice_over_15_17_furlongs_criteria_met * 100 * 6 / 11 # Calculate the percentage of horses that ran over 15-17 furlongs num_ran_over_15_17_furlongs_criteria_met = int(ran_over_15_17_furlongs == ran_over_15_17_furlongs_criteria) ran_over_15_17_furlongs_percentage = num_ran_over_15_17_furlongs_criteria_met * 100 * 6 / 11 # Calculate the overall percentage overall_percentage = age_percentage + rating_percentage + won_last_race_percentage + graded_win_percentage + \ two_wins_this_season_percentage + run_at_course_percentage + \ top_3_in_betting_percentage + two_wins_over_hurdles_percentage + \ run_4_times_over_hurdles_percentage + won_twice_over_15_17_furlongs_percentage + \ ran_over_15_17_furlongs_percentage return overall_percentage # Add a new column with the calculated percentages df['Winning Percentage'] = df.apply(calculate_percentage, axis=1) # Define color mapping color_mapping = {True: 'background-color: green', False: 'background-color: red'} # Apply formatting to each column formatted_df = df.style.applymap(color_mapping.get, subset=['Age meets criteria?', 'Rating meets criteria?', 'Won Last Race?', 'Graded Win?', 'Two Wins This Season?', 'Run at Course?', 'Top 3 in Betting?', 'Two Wins Over Hurdles?', 'Run 4 Times Over Hurdles?', 'Won Twice Over 15-17 Furlongs?', 'Ran Over 15-17 Furlongs?', 'Winning Percentage']) # Display the formatted dataframe with the new column st.dataframe(formatted_df)
关键修正点
- 移除
color_mapping定义行的多余缩进,使其与df['Winning Percentage'] = ...保持同级缩进 - 将
return overall_percentage缩进调整到函数内部级别 - 修正函数内关于“跑过4次跨栏”计算代码行的多余缩进
内容的提问来源于stack exchange,提问作者liambh
相关产品推荐
相关产品推荐

