Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The example code below shows two types of loops that can be coded using a while loop. The first is a sentinel loop that will

The example code below shows two types of loops that can be coded using a while loop.

The first is a sentinel loop that will repeat until the user enters the sentinel value. The

second loop is an input validation loop that will repeat until the user enters a value that

falls between a minimum and maximum value

Code

#Constants

MIN = 1

MAX = 100

SENTINEL = 0

def main():

count = 0 #counter variable

total = 0 #accumulator variable

int_value = get_integer() #priming read

while int_value != SENTINEL:

count += 1

total += int_value

int_value = get_integer() #loop read

print('Total number of values eneter is', count)

print('The sum of the values entered is', total)

print('The avrage value is', total/count)

def get_integer():

num_input = int(input('Enter a value between 1 and 100, 0 to

stop: '))

good_data = MIN <= num_input <= MAX or num_input ==

SENTINEL

while not good_data:

print('Value out of range')

num_input = int(input('Enter a value between 1 and 100,

0 to stop: ': '))

good_data = MIN <= num_input <= MAX or num_input ==

SENTINEL

return num_input

main()

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_2

Step: 3

blur-text-image_3

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

Analyzing Qualitative Data Systematic Approaches

Authors: H Russell Bernard, Gery W Ryan

1st Edition

0761924906, 9780761924906

More Books

Students also viewed these Psychology questions

Question

What is Bacons approach to scientific methodology?

Answered: 1 week ago