Как я могу сделать свой код так , чтобы мне не пришлось использовать все операторы def, if и elif
menu_Cheap = [['1. Shark ',4.10,], ['2. Flounder',4.10,], ['3. Cod ',4.10,], ['4. Gurnet ',4.10,], ['5. kahawai ',4.10,], ['6. Trevall ',4.10,]] menu_Deluxe = [['7. Snapper ',7.20,], ['8. Pink Salmon ',7.20,], ['9. Tuna ',7.20,], ['10.Smoked Marlin',7.20,], ['11.Clown fish ',7.20,], ['12.Surgeonfish ',7.20,]] DELIVERY = 5 PICKUP = -1.05 MAX_FISH = 7 print('\nThese are the menu items and their prices \n') for x in menu_Cheap: print(x) for y in menu_Deluxe: print(y) def Shark(): print("you have choosen Shark ") def Flounder(): print("you have choosen Flounder ") def Cod(): print("you have choosen Cod ") def Gurnet(): print("you have choosen Gurnet ") def Kahawai(): print("you have choosen Kahawai ") def Trevally(): print("you have choosen Trevally ") def Snapper(): print("you have choosen Snapper ") def Pink_Salmon(): print("you have choosen Pink Salmon ") def Tuna(): print("you have choosenTuna ") def Smoked_Marlin(): print("you have choosen Smoked_Marlin ") def Clown_Fish(): print("you have choosen Clown fish ") def Surgeonfish(): print("you have choosen Surgeonfish ") counter = 0 loop = int(input("\nPlease enter the total amount of fish you would like to order\n")) while counter < loop : try: selection = int(input("\nPlease enter the type of fish you would like to order\n")) if selection == 1: Shark() counter = counter + 1 elif selection == 2: Flounder() counter = counter + 1 elif selection == 3: Cod() counter = counter + 1 elif selection == 4: Gurnet() counter = counter + 1 elif selection == 5: Kahawai() counter = counter + 1 elif selection == 6: Trevally() counter = counter + 1 elif selection == 7: Snapper() counter = counter + 1 elif selection == 8: Pink_Salmon() counter = counter + 1 elif selection == 9: Tuna() counter = counter + 1 elif selection == 10: Smoked_Marlin() counter = counter + 1 elif selection == 11: Clown_Fish() counter = counter + 1 elif selection == 12: Surgeonfish() counter = counter + 1 else: print("please enter a values between 1 and 12 ") except ValueError: print("No strings ") exit print (Surgeonfish())
Что я уже пробовал:
мой код очень неэффективен и мне было интересно, может ли кто-нибудь дать мне некоторые идеи
Richard MacCutchan
Когда вы запрашиваете выбор рыбы, вы можете использовать возвращаемое целое число для выбора названия рыбы из меню. Нет необходимости во всех этих операторах if/elif и методах, которые печатают имена рыб. Кроме того, зачем вам нужно два меню, когда все эти пункты были бы прекрасны в одном.
Member 14176934
не могли бы вы привести пример пожалуйста
Richard MacCutchan
Смотрите мое решение ниже.