我想显示用户在列表中验证后输入的品牌的型号。当我将car_model_choice函数放在car_brand_choice之外时,它会显示模型,但无论用户选择如何,它都会显示模型。
由于所需的变量(选择)是局部变量,因此无法在其他任何地方使用,但它是显示正确模型所需的。我尝试在car_brand_choice结束时使用return但结束了程序。
我还想过,矩阵可能会有所帮助,但不确定这是如何工作的,因为每个品牌会有多个型号。
我还会根据需要提供更多代码。
如果这个问题重复,我会提前道歉,但我无法在任何地方找到它。
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
答案 0 :(得分:0)
使用每种型号的价格进行编辑。不确定我有你的ideia /问题,但你可以有一个品牌词典=>模型,并将品牌作为模型选择的参数:
car_dict = {'brand1': ['brand1_model1', 'brand1_model2'],
'brand2': ['brand2_model1', 'brand2_model2'],
'brand3': ['brand3_model1', 'brand3_model2']}
有不同的方法,但这里有一个想法:
price_dict = {'brand1_model1' => 20000, 'brand1_model2' => 22000,
'brand2_model1' => 18000, 'brand2_model2' => 21000,
'brand3_model1' => 19000, 'brand3_model2' => 24000}
然后,
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])