Skip to content Skip to sidebar Skip to footer

Sentinel Loops In Python

So I am getting inputs to store into a list from the user and I am using a sentinel loop to continuously ask the user to input a number. The issue that arises is when I use 'Stop'

Solution 1:

userInput.upper() != "Stop":

will always be True: 'stop'.upper() is 'STOP'.

if you want your loop to terminate when the user enters any capitalized version of 'stop' you should write

while userInput.upper() != "STOP":
    ....

and it may be sensible to catch other things a user could enter with

userIntput = input("")
try:
    nums.append(int(userInput))
except ValueError:
    # somehow handle what should happen here...

Post a Comment for "Sentinel Loops In Python"