Python Forum
How to Stop Sentinel Value from Entering Final Output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Stop Sentinel Value from Entering Final Output
#1
I am writing a program using while loop to determine the highest and lowest of a user's inputted numbers, allowing the user to enter as many numbers as they wish, before using the sentinel value of -1 to exit. Then, the program should print the lowest and the highest inputted values. My problem is, the sentinel value keeps entering the loop as an input value and is being considered part of the range. If the user does not enter a number below my sentinel value of -1, then the sentinel value is what appears as the lowest. I've spent time trying to fix it but without success. Does anyone have any ideas as to why this is happening and what should be done to mend it? Here is my code:

minimum = int()
highest = int()
while(True):
    num = int(input("Enter a Number (Use -1 to Stop): " ))  
    if highest < num:
        highest = num
    if minimum > num:
        minimum = num  
    if(num == -1):
        break      
   
print("The interval is",minimum,"-", highest)
Reply
#2
Move lines 9 and 10 above line 5.
Reply
#3
If you enter not a number, it will raise a ValueError.
If you enter for example abc, int can't convert this to an integer.
Catch the ValueError
Ask the user to use q or Q to quit the program.


# usually it's not good to
# set a name to None
# and afterwards to an integer
minimum = None # not set yet
maximum = None # not set yet
# if you forget this, you'll run
# into a TypeError, happens for example with comparison < or >


while True: # <- no parenthesis needed
    user_input = input("Enter a Number (q|Q to quit): ")
    if user_input.lower() == 'q':
        break
    try:
        num = int(user_input)
    except ValueError:
        print(user_input, 'is not a valid integer')
        # ask again for user input
        continue
    if minimum is None:
        # the first time minimum and maximum
        # is None
        # Just setting minimum
        # and maximum to the first user input
        # this this will never again executed
        minimum = num
        maximum = num
        # the code afterwards will be executed
        # but does no change to the value
    if maximum < num:
        maximum = num
    if minimum > num:
        minimum = num
    # fancy format string: Python 3.6+
    # first minimum and maximum is equal
    print(f"The interval is {minimum} -> {maximum}")


print('Program ends here')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Another approach would be use list and built-in min(), max() functions. With Python 3.8 walrus operator one can write:

answers = []

while (answer := input('Enter a number (q|Q) to quit): ').lower()) != 'q':
    try:
        answers.append(int(answer))
    except ValueError:
        print(f'Expected integer but got {answer!r}')

if answers:
    print(f'Mininum value {min(answers)} and maximum value {max(answers)}')
else:
    print('No integers entered!')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,073 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  problem in entering address of a file in input akbarza 0 649 Oct-18-2023, 08:16 AM
Last Post: akbarza
  syntaxerror when entering a constructor MaartenRo 2 1,979 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  How do I stop this fast factorization program from printing beyond the 3rd output? Pleiades 6 3,836 Dec-07-2019, 08:43 PM
Last Post: Pleiades
  Blending calculator from final product xerxes106 0 1,611 Dec-05-2019, 10:32 AM
Last Post: xerxes106
  Error when entering letter/character instead of number/integer helplessnoobb 2 7,057 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  Program not entering if statement on false. Scottx125 4 2,941 Nov-12-2018, 06:30 PM
Last Post: Scottx125
  Entering an expression with "input" command johnmnz 3 3,487 Sep-01-2017, 05:20 PM
Last Post: metulburr
  Trouble when entering the number sylas 24 14,893 Apr-02-2017, 01:09 PM
Last Post: sparkz_alot
  How can I make a sentinel value NOT be initialized in a class/method - OOP? netrate 2 3,512 Jan-14-2017, 07:34 AM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020