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
這就是正是我一直在尋找。如果我想更深入一些,如同演出定價等,這個概念是否適用? – user5105018
是的,看我上面的編輯,希望它有幫助。 (請考慮接受我的回答)。 – antonioduarte