How to create a dictionary using less if elif statements for a "dictionary"

Viewed 63

In the program I'm revising, I'm making a calculator/dictionary of sorts. Ive refined the basis for each module, but now I'm stuck trying to figure out how exactly to go about setting it up so that it can actually take that input, then give you a value for what materials are needed to craft the item, and then calculate how many items you need to craft that item in bulk. 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

etc.

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

The original used a bunch of if elif statements, but as stated in other questions I've asked, I'm trying to cut down on those. Not sure if its possible in this senario, but it seems to have been with the other portions of the revision.

1 Answers

In order to do this, Create a variable that will function as a dictionary.

block_craft

Then assign the block to be crafted, along with the values of required materials

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

Then go to where the choice and checker is located, and change it to look like this

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

Then when it is run, pass 1 to the input. It will give the first item, along with the correct definition. Then, add a second craft item, with materials

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

Then, run it again, this time subbing 1 for 2. It should provide the correct item, along with the definition

Related