用户提示输出捕获错误值

问题描述 投票:0回答:0

我想创建一个用户输入提示,要求输入

label
,然后是
age range
,最后是
type_of_change
的可选字段。但是,在输出中,有许多
ids
具有
type_of_change
作为非中断,即使在提示中输入
Breaking
也是如此。我不确定我哪里出错了。

这是我使用的代码:

import pandas as pd
import numpy as np

# Define valid label options
valid_labels = ['major', 'minor', 'patch', 'nan']

label = input(f"Enter the label ({', '.join(valid_labels)}): ")
age_range = input("Enter the age range (e.g. '20-30'): ")
use_type_of_change_filter = input("Do you want to filter by type of change? (y/n): ")
if use_type_of_change_filter.lower() == 'y':
    type_of_change = input("Enter the type of change (breaking/non-breaking): ")
else:
    type_of_change = None

def filter_data(label, age_range, type_of_change=None):
    if label.lower() == 'nan':
        filtered_df = df[df['label'].isna() & df['Age'].between(*map(int, age_range.split('-')))]

    elif label in valid_labels:
        age_filter = df['Age'].between(*map(int, age_range.split('-')), inclusive=True)
        if type_of_change is not None:
            filtered_df = df[(df['label'] == label) & (df['type_of_change'] == type_of_change) & age_filter]
        else:
            filtered_df = df[(df['label'] == label) & age_filter]
    else:
        print(f"Invalid label option '{label}'. Valid options are: {', '.join(valid_labels)}")
        filtered_df = pd.DataFrame()
    return filtered_df

filtered_df = filter_data(label, age_range, type_of_change)

if not filtered_df.empty:
    unique_api_spec_ids = filtered_df['api_spec_id'].unique()
    print(unique_api_spec_ids)
else:
    print("No api_spec_ids match your input.")

if not filtered_df.empty:
    unique_api_spec_ids = filtered_df['api_spec_id'].unique()
    print(len(unique_api_spec_ids))
else:
    print("No api_spec_ids match your input.")

任何建议或想法将不胜感激。

python pandas user-input
© www.soinside.com 2019 - 2024. All rights reserved.