簡體   English   中英

根據用戶選擇,顯示python品牌模型

[英]Based on user selection, display brand models python

我想顯示在列表中經過驗證后用戶輸入的品牌的模型。 當我將car_model_choice函數放置在car_brand_choice之外時,它會顯示模型,但是無論用戶選擇如何,它都會顯示模型。

由於所需的變量(選擇)是局部變量,因此無法在其他任何地方使用,但這是顯示正確模型所需的。 我嘗試在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

對每種型號的價格進行編輯 不確定我是否收到您的想法/問題,但是您可以擁有一個brand => models的字典,然后將brand作為參數傳遞給模型選擇:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM