I'm creating my first program on python. The objective is to get an output of trip cost. In the below code I want python to throw an error and ask user to retry if the input is not a part of the dictionary.
I tried using while True but when I use the code it does makes me retry on a wrong input but does not throw an error intimating the user.
c = {"Charlotte": 183, "Tampa": 220, "Pittsburgh": 222, "Los Angeles": 47} def plane_ride_cost(): city = '' while True: city = input("Name of the city: ") if city in c.keys(): return c[city] break else: print ("Invalid input please try again") plane_ride_cost() Output: Name of the city: Hyderabad Name of the city: if you notice it takes the entry and then asks me to retry without the intimation.
breakbecause using areturnalready makes you escape the loop, you also do not need to initializecity = ''because you will overwrite it right after with your input.