python 如何使用较少的if-elif语句创建字典

k7fdbhmy  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(168)

在我正在修改的程序中,我正在做一个计算器/字典。我一直在尝试输入,然后给予你一个值,什么材料需要工艺的项目,然后计算多少项目,你需要大量工艺的项目。
Original Calculator

ArmoredGlass = ("10 Glass, 2 Net Block, and 1 Ember for 10 Armored Glass [10 Sec. Craft Time]")
def blocks():
    print ("Here is the list of Craftable Blocks: \n")
    print ("Concrete1 \nWood1 \nMetal1 \nBarrierBlock \nTileBlock \nBrickBlock \nGlassBlock \nGlassTile \nPathLight \nCardBoard \nWood2 \nWood3 \nMetal2 \nMetal3 \nConcrete2 \nConcrete3 \nExtrudedMetal \nBubblePlastic \nCarpet \nNet \nSolidNet \nPunchedSteel \nRestroomBlock \nDiamondPlate \nSand \nArmoredGlass")
    time.sleep(2)
    while choice == 0:
        input_str = "\n \n Please choose a block to craft \n[ Please use same Capitalization and spaces] \n "
        choice = input(input_str)
        if choice == "Concrete1":
                print (Concrete1)
                ScrapStone = 5
                WaterEmpty = 5
                ChemicalEmpty = 5
                crafttime = 10

                singlecraft = 10

                craft = True
                bulk = True
        elif choice == "Wood1":
                print (Wood1)
                ScrapWood = 15
                crafttime = 10
                singlecraft = 10

                craft = True
                bulk = True
        elif choice == "Metal1":
                print (Metal1)
                ScrapMetal = 15
                crafttime = 10
                singlecraft = 10

等等的。
Revised Calculator

block = ["Concrete_1","Concrete_2","Concrete_3","Wood_1","Wood_2","Wood_3","Metal_1","Metal_2","Metal_3","Barrier_Block","Extruded_Metal","Tile_Block","Brick_Block","Glass_Block","Glass_Tile","Path_Light","Cardboard","Bubble_Plastic","Carpet","Net_Block","Solid_Net","Punched_Steel","Restroom_Block","Diamond_Plate","Sand","Armored_Glass"]
    for x in block:
        print(listitem, x)

        listitem += 1
        time.sleep(0.125)
    while choice == 0:
        choice = input("Choose a block (Enter only the Number):")
        if not choice.isalpha(): # Makes sure that the input is a number and not a string.
            choice = int(choice)
        else:
            choice = 0
            print("Thats not a number. Choose a number Numbnuts.")
        if choice in range (1,26+1): # reduces the if elif else chunk to a single if else statement. 26+1 allows 26 to be a value given, while allowing past the choice-1
            idx = choice - 1
            print("\n", block[idx])
        else:
            print("Please choose one of the given values.") # Error catch to prevent program from crashing due to a mispelled word or someone thinking their smart and trying to break the code
            choice = 0 # Resets the value to 0 so the loop repeats

原始版本使用了一堆if-elif语句,但我正试图减少这些语句。部分翻修手术可能发生。
如何使用较少的if-elif语句创建字典?

bbuxkriu

bbuxkriu1#

为了做到这一点,创建一个变量,它将作为一个字典。

block_craft

然后指定要制作的块,沿着所需材料的值

block_craft = {"Concrete_1" : {"ScrapStone" : 5, "WaterEmpty" : 5, "ChemicalEmpty" : 5}}

然后转到选择和检查器所在的位置,并将其更改为如下所示

if choice in range (1,26+1): 
            idx = choice - 1
            print("\n", block[idx])
            print("\n", block_craft[block[idx]])

然后当它运行时,将1传递给输入。它将给予第一项沿着正确的定义。然后,添加第二个工艺品,材料

block_craft = {"Concrete_1":{"ScrapStone" : 5, "WaterEmpty" : 5, "ChemicalEmpty" : 5}, "Concrete_2":{"Concrete1Empty" : 15, "Metal1Empty" : 2}}

然后,再次运行它,这次将1替换为2。它应该提供正确的项沿着定义

相关问题