简体   繁体   中英

Function that executes based on user selection

How do I make a function called def Toyota() that checks the user input and a user select cars that starts with Toyota , it will print words like "This is a good car!" or dive in further to the model of the Toyota and if user selects car that starts with Prius, it will print "This is very gas efficient car!" and call it on my def main() .

sData.csv

import pandas
# reads in vehicle Data
df = pandas.read_csv('sData.csv')
pandas.set_option('display.max_columns', None)
pandas.set_option('display.width', 400)

def get_choice(data, column):

    #Gets user choice
    nums = [val for val in range(len(df[column].unique()))]
    choices = list(zip(nums, data[column].unique()))
    print("Select '%s' of the car\n" % column)
    for v in choices:
        print("%s.  %s" % (v))
    user_input = input("Answer: ")
    user_answer = [val[1] for val in choices if val[0]==int(user_input)][0]
    print("'%s' = %s\n" % (column, user_answer))
    return user_answer

def Toyota():
    if df.loc[(df["make"] == "Toyota")]:
        print("This is a good car")
    if df.loc[(df["model"] == "Prius")]:
        print("This is a gas efficient car!")
    else:
        pass

def main():
    make_input = get_choice(data=df, column="make")
    filtered_makes = df.loc[df["make"] == make_input]
    model_input = get_choice(data=filtered_makes, column="model")
    filtered_model = df.loc[df["model"] == model_input]
    year_input = get_choice(data=filtered_model, column="year")
    newdf = df.loc[(df["make"] == make_input) & (df["model"] == model_input) & (df["year"] == year_input)]
    print(newdf)
    Toyota()


if __name__ == "__main__":
    main()

create two dictionaries of desired answers:

model = {
 "prius":"your text"
}
make={
"toyota":"your text"
}

and then simply print:

def Toyota():
    if df["make"] in make:
        print(make.get(df["make"]))

the same follows for models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM