# 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 = [{"Q":0},{"N":5,"S":4,"E":3,"Q":0},{"N":5,"Q":0},{"W":1,"Q":0},
{"W":2,"N":1,"Q":0},{"S":1,"W":2,"Q":0}]
# 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()
# print()
# if direction in exits[loc]:
# loc = exits[loc][direction]
# else:
# print("You cannot go in that direction")
#the 1st challenge here is to convert the exit list to dictionary,
#list index can be converted to dictionary ,by making the list index no
# to corresponding dictionary key in this case
#
#
# 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}}
# 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()
# print()
# if direction in exits[loc]:
# loc = exits[loc][direction]
# else:
# print("You cannot go in that direction")
#the 2nd challenge is to make use of the vocabulary,
#ie user may not type in the exact letters like N,S,W,E,
#user can type north,south as well
#
# 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}}
#
# vocabularyvar = {"North":"N","South":"S","East":"E","WEST":"W","QUIT":"Q"}
#
# 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)
# if len(direction)>0:
# if direction in vocabularyvar:
# direction = vocabularyvar[direction].upper()
# print(direction)
# print()
# if direction in exits[loc]:
# loc = exits[loc][direction]
# else:
# print("You cannot go in that direction")
#
# split command,will split the string by default by space,the result will be of list type
# 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,which is wild"}
#
# print(location[0])
# print(location[0].split())
# print(location[5].split(","))
# print(" ".join(location[0].split()))
#using the split command in the above example and checking whether one of the user entered words,
#matches with the exits,like if the user enters "I Prefer South"/"I want to go north"
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}}
vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q"}
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")
Saturday, 17 February 2018
Python Dictionaries Challenge - Day 8(After 1 Week)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment