[英]Based on user selection, display brand models python
I want to display the models for the brand the user has inputted after it has been verified in the list. 我想显示在列表中经过验证后用户输入的品牌的模型。 When I put the car_model_choice function outside car_brand_choice it shows the models but it shows the models regardless of user selection. 当我将car_model_choice函数放置在car_brand_choice之外时,它会显示模型,但是无论用户选择如何,它都会显示模型。
Since the variable needed (selection) is a local variable it can't be used anywhere else but it is what is needed to display the correct models. 由于所需的变量(选择)是局部变量,因此无法在其他任何地方使用,但这是显示正确模型所需的。 I tried using a return at the end of car_brand_choice but that ended the program. 我尝试在car_brand_choice的末尾使用返回值,但这结束了程序。
I also thought maybe a matrix might help but not sure how that would work since there will be multiple models per one brand. 我还认为矩阵可能会有所帮助,但不确定如何运作,因为每个品牌会有多个型号。
I will provide more code as needed also. 我还将根据需要提供更多代码。
I apologize in advance if this question is duplicated but I could not find it anywhere. 如果这个问题重复出现,我事先表示歉意,但我找不到任何地方。
def car_brand_choice():
time.sleep(1.5)
print "These are the car brands I can provide to you.\n"
print "\n".join(car_brands)
selection = raw_input("\nNow is your chance to pick which brand you want. ").title()
print "You selected %s \n" %selection
print "I have to verify your selection is valid.\n"
time.sleep(10)
if selection in car_brands:
print "Valid selection. You may continue.\n"
#Display brand models after verification
car_model_choice()
else:
print "Not valid selection."
print "Go back and select a valid brand.\n"
car_brand_choice()
def car_model_choice():
print "Select your model."
selection = "\n".join(kia_car)
print selection
Edit with price for each model . 对每种型号的价格进行编辑 。 Not sure I got your ideia/question, but you can have a dictionary of brand => models, and pass the brand as argument for model selection: 不确定我是否收到您的想法/问题,但是您可以拥有一个brand => models的字典,然后将brand作为参数传递给模型选择:
car_dict = {'brand1': ['brand1_model1', 'brand1_model2'],
'brand2': ['brand2_model1', 'brand2_model2'],
'brand3': ['brand3_model1', 'brand3_model2']}
There are different approaches, but here is one ideia: 有不同的方法,但是这里有一个想法:
price_dict = {'brand1_model1' => 20000, 'brand1_model2' => 22000,
'brand2_model1' => 18000, 'brand2_model2' => 21000,
'brand3_model1' => 19000, 'brand3_model2' => 24000}
Then, 然后,
def car_brand_choice():
time.sleep(1.5)
car_brands = car_dict.keys()
print "These are the car brands I can provide to you.\n"
print "\n".join(car_brands) # Show brands options
selection = raw_input("\nNow is your chance to pick which brand you want. ").title()
print "You selected %s \n" %selection
print "I have to verify your selection is valid.\n"
time.sleep(10)
selection = selection.lower()
if selection in car_brands:
print "Valid selection. You may continue.\n"
#Display brand models after verification
car_model_choice(selection)
else:
print "Not valid selection."
print "Go back and select a valid brand.\n"
car_brand_choice()
def car_model_choice(brand):
print "Select your model."
for model in car_dict[brand]: # Show model options
print model
model_selection = raw_input("\nNow is your chance to pick which model you want. ").title()
print "You selected a " + brand + " model " + model_selection + " by " + str(price_dict[model_selection])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.