2015-07-12 52 views
0

我想要顯示用戶在列表中驗證後輸入品牌的模型。當我將car_model_choice功能放在car_brand_choice之外時,它會顯示模型,但它會顯示模型,而不管用戶選擇。基於用戶選擇,顯示品牌模型python

由於變量需要(選擇)是一個局部變量,所以不能在其他任何地方使用,但它是顯示正確模型所需的。我嘗試在car_brand_choice結尾處使用返回,但結束了該程序。

我也認爲矩陣可能會有所幫助,但不知道該如何工作,因爲每個品牌都會有多種型號。

我會根據需要提供更多的代碼。

如果這個問題重複,但我無法找到任何地方,我提前道歉。

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

與價格編輯每個模型。不知道我有你IDEIA /問題,但可以有你的品牌=>車型的字典,並通過品牌作爲參數進行模型的選擇:

​​

有不同的方法,但這裏是一個IDEIA:

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]) 
+0

這就是正是我一直在尋找。如果我想更深入一些,如同演出定價等,這個概念是否適用? – user5105018

+0

是的,看我上面的編輯,希望它有幫助。 (請考慮接受我的回答)。 – antonioduarte

相關問題