hours = int(input(\'Enter number of hours worked: \')) # insert input validation
ID: 3860446 • Letter: H
Question
hours = int(input('Enter number of hours worked: '))
# insert input validation loop here
gross_pay = hours * 10
print('Gross pay:', gross_pay)
Suppose we want the program to ask for number of hours worked repeatedly until it is within the range of 0 to 168.
Which of the following is the best input validation loop?
while hours < 0 or hours > 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
while hours >= 0 and hours <= 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
while hours >= 0 or hours <= 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
while hours < 0 or hours > 168:
print('ERROR: Number of hours worked must be between 0 and 168')
A.while hours < 0 or hours > 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
B.while hours >= 0 and hours <= 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
C.while hours >= 0 or hours <= 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
D.while hours < 0 or hours > 168:
print('ERROR: Number of hours worked must be between 0 and 168')
Explanation / Answer
Option A:
while hours < 0 or hours > 168:
print('ERROR: Number of hours worked must be between 0 and 168')
hours = int(input('Enter number of hours worked last week: '))
Explanation:
1. The condition hours<0 checks for the hours shouldn't be less than 0,
2. The condition hours >168 checks for hours shouldn't be greater than 168.
3. 'OR' clause checks here that if any of the above two conditions fails, it will print the error message. And ask the user of input again and repeats the loop.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.