[英]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!" 我该如何创建一个名为def Toyota()
的函数来检查用户输入,然后用户选择以 Toyota 开头的汽车,它会显示“这是一辆好车!”之类的文字。 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!" 或进一步研究丰田的车型,如果用户选择以普锐斯(Prius) 开头的汽车,它将打印“这是节油型汽车!” and call it on my def main()
. 并在我的def main()
上调用它。
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 对于模型也是如此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.