I’m new to Python. Here I’ve written a Python program which retrieves values from the map.
As of now it’s working fine.
When user enters “M” it will retrieve it will ask for main no and again it asks for user input and the depending on the user input it retrieves values from the dictionary & so on.
Now my concern is
- How can I exit from the loop.
- What if I give an invalid input when it asks for M/N?
- What if I give it an invalid input when it asks for Main or Name?
Can anyone help me with this?? I want to know How to implement above 3 conditions. I implemented these conditions in Java and I don’t know how to do it in Python.
streetno={"1":"Sachin Tendulkar","2":"Sehwag","3":"Dravid","4":"Dhoni","5":"Kohli"}
streetname = dict((y,x) for x,y in streetno.items())
while True:
inp= raw_input('Enter a M/N:')
if inp=="M" or inp=="m":
key=raw_input( "Enter the main number :")
result=streetno[key]
else:
key = raw_input("Enter the street name: ")
result = streetname[key]
print result
1: You can break out of a for or while loop with
break.2/3: Account for those exceptions. We can’t really tell you how to deal with exceptions since it depends on what you want, but here’s one way to get you started..