Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Python program to evaluate a job candidate based on a few pieces of information, and print out a message of the result 5.2

Write a Python program to evaluate a job candidate based on a few pieces of information, and print out a message of the result 5.2 Coding Steps Assume the user will type acceptable answers to all input queries 1. Call a function to print a message to ask the user if they have skill in a particular, challenging part of application development (your choice which part) Read in the users typed answer (assume the answer will be the string yes or no). If the answer is no, call a function to print a message to the user indicating that they will not be considered for the position, and then call a function to exit your program To prompt the message and read the input, try a call to the input function. To check the users answer, try an if statement. The if test can check whether the value of the typed answer string is equal to the string yes. Within the if statements block: Call the print function to print the bad news To leave the program, try calling the built in exit function See ife.py. 2. Call a function to prompt the user to type in their years of work experience (an integer) and store this value into its own variable. To ensure the result is stored as a number, put the input function call statement as a parameter to a call to the eval function so that eval will accept the result of the input function call, as in myexp = eval(input(Type your...)) the eval function will evaluate its parameter as an expression and return the result 3. Call a function to prompt the user to type in their Python skill (assume they will type the string yes or no) and store this value into its own variable Hint: See the earlier step that reads in a typed value. No eval function call is needed 4. Call a function to prompt the user to type in their highest academic degree (assume they will type the string MS for masters or BS for bachelors) and store this value into its own variable Hint: See the earlier step that reads in a typed value. No eval function call is needed 2 5. Write a decision statement to control what message is printed out for the job applicant based on whether or not they are acceptable according to the criteria below. We consider the candidate qualified in any of the following cases: the applicant has Python skill with a masters and at least 2 year of work experience the applicant has Python skill with a bachelors and at least 5 year of work experience the applicant has no Python skill with a masters and at least 3 year of work experience the applicant has no Python skill with a bachelors and at least 6 year of work experience Try an outer if else statement that checks the python skill. Within the if and else blocks you can put another 2 way if else statement to chec Are you getting enough spam in your diet? (enter yes or no): no Well Ive got plenty to spare. Lets abandon this program and go fill up Listing 2: nest.py #i t e c 2 2 7 0 n e s ti n g i f example #s e t the v al u e o f some v a r i a b l e s t o u se t o # e v al u a t e the c a r i s S p o r t s C a r = ye s mil e a g e = 40000 h a sA c ci d e n t s = no #the o u t e r i f s t a temen t s t e s t c h e c k s whether the # s t r i n g v a r i a b l e h ol d s the s t r i n g ye s i f i s S p o r t s C a r == ye s : ############################################ #I t s a s p o r t s c a r . Now, check whether i t meets my #o t h e r r e q ui r em e n t s i n t h i s i n d e n t e d bl o c k #u se a n e s t e d d e c i s i o n . The 1 s t 2 c a s e s check # two v a r i a b l e v al u e s a t once . i f h a sA c ci d e n t s == no and mil e a g e < 5 0 0 0 0: pr int ( I \ l l t a ke i t . So what i f they drove i t a l o t . ) e l i f h a sA c ci d e n t s == ye s and mil e a g e < 1 0 0 0 0: pr int ( I \ l l t a ke i t . So i t s been i n an a c ci d e n t . They c ouldn \ t have done much i n a few mile s , r i g h t ? ) e l s e :#the two p r e vi o u s t e s t s f a i l e d , s o t h i s bl o c k run s . We won t buy the c a r pr int ( Well . . . thanks f o r showing me the c a r . ) ############################################ #the t e s t f o r t h i s c a s e i s whether the # s t r i n g v a r i a b l e d oe s NOT h old the s t r i n g ye s e l i f i s S p o r t s C a r != ye s : ############################################ #I t s not a s p o r t s c a r . Now, check whether i t meets my #r e q ui r em e n t s f o r a non s p o r t s c a r #u se . . . s u r p r i s e , a n e s t e d d e c i s i o n i f h a sA c ci d e n t s == no and mil e a g e < 9 0 0 0 0: pr int ( I \ l l t a ke i t . 80000 mil e s i s the new 4 0 0 0 0. ) e l i f h a sA c ci d e n t s == ye s and mil e a g e < 5 0 0 0 0: pr int ( I \ l l t a ke i t . So what i f i t s been i n an a c ci d e n t . At l e a s t noone drove i t l i k e a r a c e c a r . ) e l s e : pr int ( Well . . . thanks f o r showing me the c a r . ) 4 ############################################ e l s e : pr int ( I t s e i t h e r a s p o r t s c a r o r i t s not . This c a s e cannot oc c u r . Go and r e t hi n k your l i f e ) Ill take it. So what if they drove it alot. 6 Sample Runs 6.1 Sample Run 1 Do you understand how to use decision statements in python ? (yes/no)no We encourage you to pursue employment elsewhere 6.2 Sample Run 2 Do you understand how to use decision statements in python ? (yes/no)yes Enter your years of work experience 3 Type whether you have Python programming experience (yes/no) yes Type in your highest academic degree (MS/BS) MS Well contact you soon End of Sample Runs 7 Part 2 7.1 Summary Write a Python program which reads lines from a file called roster.txt until finding your first name (you being the programmer) or reaching the end of the file. If your first name is in the file, you are on the list of amateur actors to audition for an acting part (!). Then, begin a second loop in which you allow the user/auditioner to type their lines for the performance until they are done. The user either types a line to say and then presses enter, or just presses enter by itself to indicate they are done, and the program ends 7.2 Non-Coding Steps Create a plain text file called roster.txt to use as input to your program. In the text file, type first names, each one their own line. For testing purposes, put your name in the file somewhere on its own line 5 7.3 Coding Steps 7.3.1 First Loop 1. Call a function to open the roster file for reading and create a file object, then save the object If the file is not found, use Exception Handling to handle the IOError. With Exception handling you put error-creating statements in the try block, and create an except block for each error Do the following in the except block for the IOError: Call a function to print a message indicating an error Exit the program To open the file, as usual call the open function as in myfile = open(filename,r) The function call can cause an IOError if the file to open is not present in the same directory as your code, so put the statement in the try block The IOError can be caught with an except block. In the except block: Print the news of the error. To exit the program, call the exit function. See exc.py for an example 2. If your program did not exit, the result of the open function (a file object attached to the file) should be saved in a variable. Write a loop to read every line from the file, printing a special message if your first name is found. In the loop, repeat the following bulleted steps until an empty line is read in from the file (an empty line means the end of the file) Write the loop header line like while test: where test is the loop test Try an (indefinite) while loop containing an if statement within its block. The if statement checks if the latest line is your 1st name See p2e.py for an example indefinite loop Call a function to read in a line from the file object and save it The readline function is available from file objects. It returns the next line from the file. The statement line = myfile.readline() would read one line from the file and save it in the variable line. Note that the line might contain a newline at end, so the strip or rstrip functions can be used to remove the extra whitespace If the string in the line matches the string that is your (you as the programmer) first name, call a function to print out an informative message about it. 6 Try an if statement to test whether the line is your first name. The header line of the if statement can be written like if line == Monty: In the indented block you can call the print function 7.3.2 Second Loop 1. After your first loop, write a new indefinite loop to repeatedly let the user type their audition lines until they indicate they are finished by simply pressing enter. Try an indefinite while loop. First, create a string variable and set its value to a non-empty test value to ensure your loop starts, like line = x To ensure the loop ends when the line is empty, the loop header line can be written like while inputline != : In the indented block put some statements to do the following: Call a function to print a message to tell the user to type their audition line and press enter, or simply press enter to end their audition Read in the user-typed string. Notice that the steps can be completed with one call. Try the input function. See loop2.py for an example Listing 3: exc.py try : #r i s k y s t a t em e n t s go i n a t r y bl o c k i f i l e =open ( some none x i s t e n t f i l e , r ) #the e x c e p t l i n e s t a t e s the e r r o r t h a t # c o ul d occur , and a v a r i a b l e name t o c a t c h # i t s i n f o rm a ti o n except IOError a s i o e : #h andle the e r r o r . . pr int ( IO E r r o r : , s t r ( i o e ) ) e x i t ( ) IO Error: [Errno 2] No such file or directory: some non-existent file 7 Listing 4: p2e.py #c a l l the open f u n c ti o n t o c r e a t e a f i l e o b j e c t # a t t a c h e d t o the f i l e t e s t , f o r r e a di n g m y fil e = open ( t e s t , r ) #c r e a t e a v a r i a b l e t o h old the l i n e from the # f i l e . Se t i t s v al u e t o something nonempty # s o t h a t our l o o p t e s t w i l l p a s s the 1 s t time # t o e n t e r the l o o p ( k i c k s t a r t ) l i n e= x #the t e s t f o r t h i s w hil e l o o p i s # based on the v al u e i n the v a r i a b l e # re ad from the f i l e . When the l i n e i s empty , # the l o o p ends while l i n e != : #c a l l the r e a d l i n e f u n c ti o n from the f i l e o b j e c t # t o g e t a l i n e from the f i l e a s a s t r i n g l i n e = m y fil e . r e a d l i n e ( ) #Note : t h e r e may be a n ewli n e c h a r a c t e r a t the end o f the s t r i n g # i f your f i l e s i n f o rm a ti o n i s on d i f f e r e n t l i n e s #C all the r s t r i p f u n c ti o n from the s t r i n g t o # remove any w hi t e s p a c e c h a r a c t e r e on r i g h t s i d e i n the s t r i n g . # s a vi n g the r e s u l t i n an o the r v a r i a b l e lineWi th ou tNewline=l i n e . r s t r i p ( ) i f lineWi th ou tNewline==Monty : pr int ( Found the name Monty i n the f i l e c a l l e d t e s t ) contents of the file test Phil Frank Monty Bob The output of p2e.py Found the name Monty in the file called test Listing 5: loop2.py #c r e a t e a v a r i a b l e t o h old the # l i n e re ad from the u s e r . Se t i t s # v al u e t o something nonempty t o e n s u r e 8 # our l o o p w i l l p a s s i t s t e s t the f i r s t time l i n e = x #i n d e f i n i t e w hil e l o o p . #The l o o p r e p e a t s u n t i l the v a r i a b l e # c o n t ai n s an empty s t ri n g , while l i n e != : l i n e = i n p u t ( Type a word Monty . Ju s t p r e s s i n g e n t e r w i l l i n p u t an empty s t r i n g and we\ l l be done l i s t e n i n g t o you : ) Type a word Monty. Just pressing enter will input an empty string and well be done listening to you: spam Type a word Monty. Just pressing enter will input an empty string and well be done listening to you: 8 Sample Runs 8.1 Sample Run 1 (file available) ####################### The amateur actors rehearsal Searching for your name on the list...(are you sure you should be acting?) Searching for your name on the list...(are you sure you should be acting?) Searching for your name on the list...(are you sure you should be acting?) Ok.. monty . Youre on the list for rehearsal. Well let you say your lines until youve had enough. Searching for your name on the list...(are you sure you should be acting?) Searching for your name on the list...(are you sure you should be acting?) Type in your line. If youre finished, just press enter: spam Type in your line. If youre finished, just press enter: is Type in your line. If youre finished, just press enter: all Type in your line. If youre finished, just press enter: that Type in your line. If youre finished, just press enter: i Type in your line. If youre finished, just press enter: am Type in your line. If youre finished, just press enter: 8.2 Sample Run 2 (file not available) IO Error: [Errno 2] No such file or directory: roster.txt End of Sample Runs 9 Other Requirements Write your scripts in a documented, organized and readable manner. 9 10 Submission Submit using the submission folder in D2L for this assignment The python script, for example smith bill hw3p2.py, is the file to submit. Do not submit text copied from the python interactive session. You must save the script as a file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

More Books

Students also viewed these Databases questions

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago