Saturday, 17 February 2018

Python Dictionaries CopyUpdate- Day 9(After 6 days)

  • Dictionary Copy Update Concept
  •   
    # filmsvar1 = {"muthu":"one of the movie in which rajini comes in double role",
    #          "basha":"Don of a don movie,a cult hit",
    #          "enthiran":"rajini's scifi movie",
    #          "murattu kalai":"movie taken in paganeri"}
    #
    # filmsvar2 = {"BLoodStonte":"Rajini's Hollywood Movie",
    #              "enthiran2":"Upcoming Movie"}
    #
    # # print(filmsvar1)
    # # print(filmsvar2)
    # # #this will update the filmsvar1 wont create a separate object
    # # filmsvar1.update(filmsvar2)
    # # print(filmsvar1)
    # # print(filmsvar2)
    #
    # #to make a copy of dictionary
    #
    # filmsvar3 = filmsvar2.copy()
    # print(filmsvar2)
    # print(filmsvar3)
    # filmsvar2.update(filmsvar1)
    # print(filmsvar2)
    # print(filmsvar3)
    #
    # #Use the old challenge and make it work even if the user types valley,road directly
    #
    # location = {0:"You are Sitting in front of comp",
    #             1:"You are in road",
    #             2:"At the top of the hill",
    #             3:"Building",
    #             4:"You are across Valley",
    #             5:"Roaming at forest"}
    # exits = {0:{"Q":0},
    #          1:{"N":5,"5":5,"S":4,"4":4,"E":3,"3":3,"Q":0},
    #          2:{"N":5,"5":5,"Q":0},
    #          3:{"W":1,"1":1,"Q":0},
    #          4:{"W":2,"2":2,"N":1,"1":1,"Q":0},
    #          5:{"S":1,"1":1,"W":2,"2":2,"Q":0}}
    #
    # vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q",
    #                  "ROAD":"1","HILL":"2","BUILDING":"3","VALLEY":"4","FOREST":"5"}
    #
    # loc=1
    # while True:
    #     availablexits = ""
    #     #we can use join to do the fllowing
    #     # for direction in exits[loc].keys():
    #     #     availablexits +=direction+ ","
    #     availablexits = ','.join(exits[loc].keys())
    #
    #     print(location[loc])
    #
    #     if loc == 0:
    #         break
    #
    #     direction = input("Available Exits are "+ availablexits).upper()
    #     if len(direction)>0:
    #         words = direction.split()
    #         for word in words:
    #             if word in vocabularyvar:
    #                 direction = vocabularyvar[word]
    #                 print(direction)
    #     print()
    #     if direction in exits[loc]:
    #         loc = exits[loc][direction]
    #     else:
    #         print("You cannot go in that direction")
    
    #the above code will work,but it will unnecessarily will show no's 1,2,3,4,5 in the
    # available exits,so we can use the copy and update funtions in dictionary to solve those issues
    
    location = {0:"You are Sitting in front of comp",
                1:"You are in road",
                2:"At the top of the hill",
                3:"Building",
                4:"You are across Valley",
                5:"Roaming at forest"}
    exits = {0:{"Q":0},
             1:{"N":5,"S":4,"E":3,"Q":0},
             2:{"N":5,"Q":0},
             3:{"W":1,"Q":0},
             4:{"W":2,"N":1,"Q":0},
             5:{"S":1,"W":2,"Q":0}}
    
    namedexits  = {1:{"5":5,"4":4,"3":3,"Q":0},
                   2:{"5":5,"Q":0},
                   3:{"1":1,"Q":0},
                   4:{"2":2,"1":1,"Q":0},
                   5:{"1":1,"2":2,"Q":0}}
    
    vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q",
                     "ROAD":"1","HILL":"2","BUILDING":"3","VALLEY":"4","FOREST":"5"}
    
    loc=1
    while True:
        availablexits = ""
        #we can use join to do the fllowing
        # for direction in exits[loc].keys():
        #     availablexits +=direction+ ","
        availablexits = ','.join(exits[loc].keys())
    
        print(location[loc])
    
        if loc == 0:
            break
        else:
            allexits = exits[loc].copy()
            allexits.update(namedexits[loc])
    
        direction = input("Available Exits are "+ availablexits).upper()
        if len(direction)>0:
            words = direction.split()
            for word in words:
                if word in vocabularyvar:
                    direction = vocabularyvar[word]
                    print(direction)
        print()
        #previously we used index ,since it it dictionary under dictionary
        if direction in allexits:
            loc = allexits[direction]
        else:
            print("You cannot go in that direction")
    
     
    

    No comments:

    Post a Comment