Sentinel Loops

In [1]:
def average3():
    _sum = 0.0
    count = 0
    x = float(input("Enter a number (negative to quit) >> "))
    
    while x >= 0:
        _sum = _sum + x
        count = count + 1
        x = float(input("Enter a number (negative to quit) >> "))
        
    print("\nThe average of the numbers is", _sum/count)
In [4]:
average3()
Enter a number (negative to quit) >> 34
Enter a number (negative to quit) >> 6
Enter a number (negative to quit) >> 34
Enter a number (negative to quit) >> 6
Enter a number (negative to quit) >> -3

The average of the numbers is 20.0
In [5]:
def average3a():
    _sum = 0.0
    count = 0
    
    try:
        x = float(input("Enter a number (negative to quit) >> "))
    except ValueError:
        print("Invalid input!")
        return
        
    while x >= 0:
        _sum = _sum + x
        count = count + 1
        try:
            x = float(input("Enter a number (negative to quit) >> "))
        except ValueError:
            print("Invalid Input!")
            continue
        
    print("\nThe average of the numbers is", _sum/count)
In [6]:
average3a()
Enter a number (negative to quit) >> 34
Enter a number (negative to quit) >> 45
Enter a number (negative to quit) >> 64
Enter a number (negative to quit) >> 24
Enter a number (negative to quit) >> 67
Enter a number (negative to quit) >> dfsdf
Invalid Input!
Enter a number (negative to quit) >> sgfg
Invalid Input!
Enter a number (negative to quit) >> sgfg
Invalid Input!
Enter a number (negative to quit) >> 35
Enter a number (negative to quit) >> -1

The average of the numbers is 52.22222222222222
In [7]:
average3a()
Enter a number (negative to quit) >> ddfdf
Invalid input!
In [8]:
def average3b():
    _sum = 0.0
    count = 0
    
    x = 1
        
    while x >= 0:
        try:
            x = float(input("Enter a number (negative to quit) >> "))
        except ValueError:
            print("Invalid Input!")
            continue
            
        _sum = _sum + x
        count = count + 1

        
    print("\nThe average of the numbers is", _sum/count)
In [9]:
average3b()
Enter a number (negative to quit) >> 023
Enter a number (negative to quit) >> 234
Enter a number (negative to quit) >> 34
Enter a number (negative to quit) >> 235
Enter a number (negative to quit) >> 63
Enter a number (negative to quit) >> 66
Enter a number (negative to quit) >> -3

The average of the numbers is 93.14285714285714
In [11]:
def average4():
    _sum = 0.0
    count = 0
    _str = input("Enter a number (<Enter> to quit) >> ")
    
    while _str != "":
        x = float(_str)
        _sum = _sum + x
        count = count + 1
        _str = input("Enter a number (<Enter> to quit) >> ")
        
    print("\nThe average of the numbers is", _sum/count)
In [12]:
average4()
Enter a number (<Enter> to quit) >> 34
Enter a number (<Enter> to quit) >> 25
Enter a number (<Enter> to quit) >> 765
Enter a number (<Enter> to quit) >> 36
Enter a number (<Enter> to quit) >> 56
Enter a number (<Enter> to quit) >> 

The average of the numbers is 183.2
In [ ]:
def average4a():
    _sum = 0.0
    count = 0
    _str = input("Enter a number (<Enter> to quit) >> ")
    
    while _str != "":
        try:
            x = float(_str)
        except ValueError:
            print("Invalid input!")
            # This pattern cannot use continue
            #continue
        else:
            _sum = _sum + x
            count = count + 1
            
        _str = input("Enter a number (<Enter> to quit) >> ")
        
    print("\nThe average of the numbers is", _sum/count)
In [ ]:
average4a()
In [ ]: