Suppose we are writing a program to calculate the gross pay of workers in a grocery store.
Which of the following Python programs will calculate each worker’s gross pay until the user does not answer ‘y’ to the question "Calculate another gross pay"?

A.
keep_going = input('Calculate another gross pay? [Enter y for yes] ')
while keep_going == 'y':
hours = float(input('How many hours did this worker work? '))
gross_pay = hours * 10
print('Gross pay:', gross_pay)

B.
keep_going = 'y'
if keep_going == 'y':
hours = float(input('How many hours did this worker work? '))
gross_pay = hours * 10
print('Gross pay:', gross_pay)
keep_going = input('Calculate another gross pay? [Enter y for yes] ')

C.
while keep_going == 'y':
hours = float(input('How many hours did this worker work? '))
gross_pay = hours * 10
print('Gross pay:', gross_pay)
keep_going = input('Calculate another gross pay? [Enter y for yes] ')

D.
keep_going = 'y'
while keep_going == 'y':
hours = float(input('How many hours did this worker work? '))
gross_pay = hours * 10
print('Gross pay:', gross_pay)
keep_going = input('Calculate another gross pay? [Enter y for yes] ')

Respuesta :

Answer:

The explaination fo this question is given below in the explanation section. However, the correct option of this question is D.

Explanation:

The correct option of this question is D

keep_going = 'y'

while keep_going == 'y':

   hours = float(input('How many hours did this worker work? '))

   gross_pay = hours * 10

   print('Gross pay:', gross_pay)

   keep_going = input('Calculate another gross pay? [Enter y for yes] ')

# this program will print in a sequence and produce the correct result as #shown in attached picture.    

Why Other options are not correct

A. This option is not correct becuase it not running in a sequence. It asked first "Calculate another gross pay enter y for yes"  . then it get started in unterminated loop and do not finish the loop.

The following output is produce by this code

Calculate another gross pay? [Enter y for yes] y                                                               How many hours did this worker work? 12                                                                                        Gross pay: 120.0                                                                                                               How many hours did this worker work? 10                                                                                        Gross pay: 100.0                                                                                                               How many hours did this worker work? 13                                                                                        Gross pay: 130.0                                                                                                               How many hours did this worker work?  

 B.  This option produce the following output

     How many hours did this worker work? 10                                                                                              Gross pay: 100.0                                                                                                                     Calculate another gross pay? [Enter y for yes] y    

In this option as you enter Y. the program get close and terminated.

C. This option produce the following out put.

   while keep_going == 'y':                                                                                                       NameError: name 'keep_going' is not defined

This option is not correct because in it keep_going is not defined.

Ver imagen asarwar