in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can also do that with tail recursion. c = {"Charlotte": 183, "Tampa": 220, "Pittsburgh": 222, "Los Angeles": 47} def plane_ride_cost(): city = input("Name of the city: ") if city in c: #try: return c[city] #except: print ("Invalid input please try again") plane_ride_cost() plane_ride_cost()

Related questions

0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am looking to write a pop-up window which asks the user to select a specific option, and if ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I need to find a message the bot has previously posted, then see the reactions on it. I don't ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I have multiple xlsx files with data in it that i want to import to separate dataframes in Python. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have multiple xlsx files with data in it that i want to import to separate dataframes in Python. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to get the values of col1 in 3 different columns with separate headers. Date/Time col1 0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a dataset, say: DateJoined Name Number DateLeft 11/03/2015 Tom 001199 11/03/2019 11/03/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    When I try to run this pygame code it instantly closes?? The window doesn't close instantly when I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I am working on a homework assignment in which we are creating a class to be used in a program to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am trying to fit some (numpy) data into python skLearn modules, but keep getting error messages. When ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have a series of lists, call them A, B, C, D, E. Now every list has 5 elements with ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have a legit situation like this where I need to implement a method that changes the incoming object ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Let's say I have the following code: from types import coroutine @coroutine def stop(): yield 1 async ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I'm working on a project which need these deep learning libraries - keras and tensorflow. Unfortunately, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me a python -m SimpleHTTPServer equivalent Python 3? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
...